Skip to content

Commit 8083b7a

Browse files
authored
feat: update solutions to lc problems: No.1602~1603 (#2477)
1 parent edd5931 commit 8083b7a

File tree

7 files changed

+107
-78
lines changed

7 files changed

+107
-78
lines changed

solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README.md

+27-23
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
### 方法一:BFS
6363

64-
BFS 层序遍历,找到 $u$ 所在层的右侧相邻节点
64+
我们可以使用广度优先搜索,从根节点开始搜索,当搜索到节点 $u$ 时,返回队列中的下一个节点
6565

6666
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。
6767

@@ -147,9 +147,15 @@ public:
147147
for (int i = q.size(); i; --i) {
148148
root = q.front();
149149
q.pop();
150-
if (root == u) return i > 1 ? q.front() : nullptr;
151-
if (root->left) q.push(root->left);
152-
if (root->right) q.push(root->right);
150+
if (root == u) {
151+
return i > 1 ? q.front() : nullptr;
152+
}
153+
if (root->left) {
154+
q.push(root->left);
155+
}
156+
if (root->right) {
157+
q.push(root->right);
158+
}
153159
}
154160
}
155161
return nullptr;
@@ -321,29 +327,27 @@ class Solution {
321327
*/
322328
class Solution {
323329
public:
324-
TreeNode* u;
325-
TreeNode* ans;
326-
int d = 0;
327-
328330
TreeNode* findNearestRightNode(TreeNode* root, TreeNode* u) {
329-
this->u = u;
331+
TreeNode* ans;
332+
int d = 0;
333+
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int i) {
334+
if (!root || ans) {
335+
return;
336+
}
337+
if (d == i) {
338+
ans = root;
339+
return;
340+
}
341+
if (root == u) {
342+
d = i;
343+
return;
344+
}
345+
dfs(root->left, i + 1);
346+
dfs(root->right, i + 1);
347+
};
330348
dfs(root, 1);
331349
return ans;
332350
}
333-
334-
void dfs(TreeNode* root, int i) {
335-
if (!root || ans) return;
336-
if (d == i) {
337-
ans = root;
338-
return;
339-
}
340-
if (root == u) {
341-
d = i;
342-
return;
343-
}
344-
dfs(root->left, i + 1);
345-
dfs(root->right, i + 1);
346-
}
347351
};
348352
```
349353

solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README_EN.md

+36-24
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737

3838
## Solutions
3939

40-
### Solution 1
40+
### Solution 1: BFS
41+
42+
We can use Breadth-First Search, starting from the root node. When we reach node $u$, we return the next node in the queue.
43+
44+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.
4145

4246
<!-- tabs:start -->
4347

@@ -121,9 +125,15 @@ public:
121125
for (int i = q.size(); i; --i) {
122126
root = q.front();
123127
q.pop();
124-
if (root == u) return i > 1 ? q.front() : nullptr;
125-
if (root->left) q.push(root->left);
126-
if (root->right) q.push(root->right);
128+
if (root == u) {
129+
return i > 1 ? q.front() : nullptr;
130+
}
131+
if (root->left) {
132+
q.push(root->left);
133+
}
134+
if (root->right) {
135+
q.push(root->right);
136+
}
127137
}
128138
}
129139
return nullptr;
@@ -200,7 +210,11 @@ var findNearestRightNode = function (root, u) {
200210

201211
<!-- tabs:end -->
202212

203-
### Solution 2
213+
### Solution 2: DFS
214+
215+
DFS performs a pre-order traversal of the binary tree. The first time we search to node $u$, we mark the current depth $d$. The next time we encounter a node at the same level, it is the target node.
216+
217+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.
204218

205219
<!-- tabs:start -->
206220

@@ -291,29 +305,27 @@ class Solution {
291305
*/
292306
class Solution {
293307
public:
294-
TreeNode* u;
295-
TreeNode* ans;
296-
int d = 0;
297-
298308
TreeNode* findNearestRightNode(TreeNode* root, TreeNode* u) {
299-
this->u = u;
309+
TreeNode* ans;
310+
int d = 0;
311+
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int i) {
312+
if (!root || ans) {
313+
return;
314+
}
315+
if (d == i) {
316+
ans = root;
317+
return;
318+
}
319+
if (root == u) {
320+
d = i;
321+
return;
322+
}
323+
dfs(root->left, i + 1);
324+
dfs(root->right, i + 1);
325+
};
300326
dfs(root, 1);
301327
return ans;
302328
}
303-
304-
void dfs(TreeNode* root, int i) {
305-
if (!root || ans) return;
306-
if (d == i) {
307-
ans = root;
308-
return;
309-
}
310-
if (root == u) {
311-
d = i;
312-
return;
313-
}
314-
dfs(root->left, i + 1);
315-
dfs(root->right, i + 1);
316-
}
317329
};
318330
```
319331

solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ class Solution {
1717
for (int i = q.size(); i; --i) {
1818
root = q.front();
1919
q.pop();
20-
if (root == u) return i > 1 ? q.front() : nullptr;
21-
if (root->left) q.push(root->left);
22-
if (root->right) q.push(root->right);
20+
if (root == u) {
21+
return i > 1 ? q.front() : nullptr;
22+
}
23+
if (root->left) {
24+
q.push(root->left);
25+
}
26+
if (root->right) {
27+
q.push(root->right);
28+
}
2329
}
2430
}
2531
return nullptr;

solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution2.cpp

+17-19
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,25 @@
1111
*/
1212
class Solution {
1313
public:
14-
TreeNode* u;
15-
TreeNode* ans;
16-
int d = 0;
17-
1814
TreeNode* findNearestRightNode(TreeNode* root, TreeNode* u) {
19-
this->u = u;
15+
TreeNode* ans;
16+
int d = 0;
17+
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int i) {
18+
if (!root || ans) {
19+
return;
20+
}
21+
if (d == i) {
22+
ans = root;
23+
return;
24+
}
25+
if (root == u) {
26+
d = i;
27+
return;
28+
}
29+
dfs(root->left, i + 1);
30+
dfs(root->right, i + 1);
31+
};
2032
dfs(root, 1);
2133
return ans;
2234
}
23-
24-
void dfs(TreeNode* root, int i) {
25-
if (!root || ans) return;
26-
if (d == i) {
27-
ans = root;
28-
return;
29-
}
30-
if (root == u) {
31-
d = i;
32-
return;
33-
}
34-
dfs(root->left, i + 1);
35-
dfs(root->right, i + 1);
36-
}
3735
};

solution/1600-1699/1603.Design Parking System/README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,20 @@ class ParkingSystem {
100100
```cpp
101101
class ParkingSystem {
102102
public:
103-
vector<int> cnt;
104-
105103
ParkingSystem(int big, int medium, int small) {
106104
cnt = {0, big, medium, small};
107105
}
108106

109107
bool addCar(int carType) {
110-
if (cnt[carType] == 0) return false;
108+
if (cnt[carType] == 0) {
109+
return false;
110+
}
111111
--cnt[carType];
112112
return true;
113113
}
114+
115+
private:
116+
vector<int> cnt;
114117
};
115118

116119
/**

solution/1600-1699/1603.Design Parking System/README_EN.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,20 @@ class ParkingSystem {
9292
```cpp
9393
class ParkingSystem {
9494
public:
95-
vector<int> cnt;
96-
9795
ParkingSystem(int big, int medium, int small) {
9896
cnt = {0, big, medium, small};
9997
}
10098

10199
bool addCar(int carType) {
102-
if (cnt[carType] == 0) return false;
100+
if (cnt[carType] == 0) {
101+
return false;
102+
}
103103
--cnt[carType];
104104
return true;
105105
}
106+
107+
private:
108+
vector<int> cnt;
106109
};
107110

108111
/**

solution/1600-1699/1603.Design Parking System/Solution.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
class ParkingSystem {
22
public:
3-
vector<int> cnt;
4-
53
ParkingSystem(int big, int medium, int small) {
64
cnt = {0, big, medium, small};
75
}
86

97
bool addCar(int carType) {
10-
if (cnt[carType] == 0) return false;
8+
if (cnt[carType] == 0) {
9+
return false;
10+
}
1111
--cnt[carType];
1212
return true;
1313
}
14+
15+
private:
16+
vector<int> cnt;
1417
};
1518

1619
/**

0 commit comments

Comments
 (0)