Skip to content

Commit 14d5068

Browse files
authored
feat: add solutions to lc problems: No.1469,1470 (doocs#3632)
* No.1469.Find All The Lonely Nodes * No.1470.Shuffle the Array
1 parent 7c25a05 commit 14d5068

File tree

16 files changed

+244
-214
lines changed

16 files changed

+244
-214
lines changed

solution/1400-1499/1469.Find All The Lonely Nodes/README.md

+64-18
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ tags:
7474

7575
<!-- solution:start -->
7676

77-
### 方法一:递归
77+
### 方法一:DFS
7878

79-
递归搜索二叉树,如果当前节点的左右子节点都不为空,则继续递归搜索左右子树;如果当前节点的左右子节点有一个为空,则将不为空的子节点的值加入结果数组中,然后继续递归搜索左右子树。
79+
我们可以使用深度优先搜索遍历整棵树,设计一个函数 $\textit{dfs}$,它的作用是遍历树中的每个节点,如果当前节点是独生节点,那么将其值加入答案数组中。函数 $\textit{dfs}$ 的执行过程如下:
8080

81-
时间复杂度 $O(n)$,其中 $n$ 为二叉树的节点个数。需要对二叉树进行一次遍历。
81+
1. 如果当前节点为空,或者当前节点是叶子节点,即当前节点的左右子节点都为空,那么直接返回。
82+
2. 如果当前节点的左子节点为空,那么将当前节点的右子节点是独生节点,将其值加入答案数组中。
83+
3. 如果当前节点的右子节点为空,那么将当前节点的左子节点是独生节点,将其值加入答案数组中。
84+
4. 递归遍历当前节点的左子节点和右子节点。
85+
86+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树中节点的个数。
8287

8388
<!-- tabs:start -->
8489

@@ -93,8 +98,8 @@ tags:
9398
# self.right = right
9499
class Solution:
95100
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
96-
def dfs(root):
97-
if root is None or (root.left is None and root.right is None):
101+
def dfs(root: Optional[TreeNode]):
102+
if root is None or root.left == root.right:
98103
return
99104
if root.left is None:
100105
ans.append(root.right.val)
@@ -135,7 +140,7 @@ class Solution {
135140
}
136141

137142
private void dfs(TreeNode root) {
138-
if (root == null || (root.left == null && root.right == null)) {
143+
if (root == null || (root.left == root.right)) {
139144
return;
140145
}
141146
if (root.left == null) {
@@ -168,15 +173,20 @@ class Solution {
168173
public:
169174
vector<int> getLonelyNodes(TreeNode* root) {
170175
vector<int> ans;
171-
function<void(TreeNode * root)> dfs;
172-
dfs = [&](TreeNode* root) {
173-
if (!root || (!root->left && !root->right)) return;
174-
if (!root->left) ans.push_back(root->right->val);
175-
if (!root->right) ans.push_back(root->left->val);
176-
dfs(root->left);
177-
dfs(root->right);
176+
auto dfs = [&](auto&& dfs, TreeNode* root) {
177+
if (!root || (root->left == root->right)) {
178+
return;
179+
}
180+
if (!root->left) {
181+
ans.push_back(root->right->val);
182+
}
183+
if (!root->right) {
184+
ans.push_back(root->left->val);
185+
}
186+
dfs(dfs, root->left);
187+
dfs(dfs, root->right);
178188
};
179-
dfs(root);
189+
dfs(dfs, root);
180190
return ans;
181191
}
182192
};
@@ -193,11 +203,10 @@ public:
193203
* Right *TreeNode
194204
* }
195205
*/
196-
func getLonelyNodes(root *TreeNode) []int {
197-
ans := []int{}
206+
func getLonelyNodes(root *TreeNode) (ans []int) {
198207
var dfs func(*TreeNode)
199208
dfs = func(root *TreeNode) {
200-
if root == nil || (root.Left == nil && root.Right == nil) {
209+
if root == nil || (root.Left == root.Right) {
201210
return
202211
}
203212
if root.Left == nil {
@@ -210,7 +219,44 @@ func getLonelyNodes(root *TreeNode) []int {
210219
dfs(root.Right)
211220
}
212221
dfs(root)
213-
return ans
222+
return
223+
}
224+
```
225+
226+
#### TypeScript
227+
228+
```ts
229+
/**
230+
* Definition for a binary tree node.
231+
* class TreeNode {
232+
* val: number
233+
* left: TreeNode | null
234+
* right: TreeNode | null
235+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
236+
* this.val = (val===undefined ? 0 : val)
237+
* this.left = (left===undefined ? null : left)
238+
* this.right = (right===undefined ? null : right)
239+
* }
240+
* }
241+
*/
242+
243+
function getLonelyNodes(root: TreeNode | null): number[] {
244+
const ans: number[] = [];
245+
const dfs = (root: TreeNode | null) => {
246+
if (!root || root.left === root.right) {
247+
return;
248+
}
249+
if (!root.left) {
250+
ans.push(root.right.val);
251+
}
252+
if (!root.right) {
253+
ans.push(root.left.val);
254+
}
255+
dfs(root.left);
256+
dfs(root.right);
257+
};
258+
dfs(root);
259+
return ans;
214260
}
215261
```
216262

solution/1400-1499/1469.Find All The Lonely Nodes/README_EN.md

+66-16
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,16 @@ All other nodes are lonely.
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: DFS
71+
72+
We can use Depth-First Search (DFS) to traverse the entire tree. We design a function $\textit{dfs}$, which traverses each node in the tree. If the current node is a lone child, we add its value to the answer array. The execution process of the function $\textit{dfs}$ is as follows:
73+
74+
1. If the current node is null, or the current node is a leaf node (i.e., both the left and right children of the current node are null), then return directly.
75+
2. If the left child of the current node is null, then the right child of the current node is a lone child, and we add its value to the answer array.
76+
3. If the right child of the current node is null, then the left child of the current node is a lone child, and we add its value to the answer array.
77+
4. Recursively traverse the left and right children of the current node.
78+
79+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
7180

7281
<!-- tabs:start -->
7382

@@ -82,8 +91,8 @@ All other nodes are lonely.
8291
# self.right = right
8392
class Solution:
8493
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
85-
def dfs(root):
86-
if root is None or (root.left is None and root.right is None):
94+
def dfs(root: Optional[TreeNode]):
95+
if root is None or root.left == root.right:
8796
return
8897
if root.left is None:
8998
ans.append(root.right.val)
@@ -124,7 +133,7 @@ class Solution {
124133
}
125134

126135
private void dfs(TreeNode root) {
127-
if (root == null || (root.left == null && root.right == null)) {
136+
if (root == null || (root.left == root.right)) {
128137
return;
129138
}
130139
if (root.left == null) {
@@ -157,15 +166,20 @@ class Solution {
157166
public:
158167
vector<int> getLonelyNodes(TreeNode* root) {
159168
vector<int> ans;
160-
function<void(TreeNode * root)> dfs;
161-
dfs = [&](TreeNode* root) {
162-
if (!root || (!root->left && !root->right)) return;
163-
if (!root->left) ans.push_back(root->right->val);
164-
if (!root->right) ans.push_back(root->left->val);
165-
dfs(root->left);
166-
dfs(root->right);
169+
auto dfs = [&](auto&& dfs, TreeNode* root) {
170+
if (!root || (root->left == root->right)) {
171+
return;
172+
}
173+
if (!root->left) {
174+
ans.push_back(root->right->val);
175+
}
176+
if (!root->right) {
177+
ans.push_back(root->left->val);
178+
}
179+
dfs(dfs, root->left);
180+
dfs(dfs, root->right);
167181
};
168-
dfs(root);
182+
dfs(dfs, root);
169183
return ans;
170184
}
171185
};
@@ -182,11 +196,10 @@ public:
182196
* Right *TreeNode
183197
* }
184198
*/
185-
func getLonelyNodes(root *TreeNode) []int {
186-
ans := []int{}
199+
func getLonelyNodes(root *TreeNode) (ans []int) {
187200
var dfs func(*TreeNode)
188201
dfs = func(root *TreeNode) {
189-
if root == nil || (root.Left == nil && root.Right == nil) {
202+
if root == nil || (root.Left == root.Right) {
190203
return
191204
}
192205
if root.Left == nil {
@@ -199,7 +212,44 @@ func getLonelyNodes(root *TreeNode) []int {
199212
dfs(root.Right)
200213
}
201214
dfs(root)
202-
return ans
215+
return
216+
}
217+
```
218+
219+
#### TypeScript
220+
221+
```ts
222+
/**
223+
* Definition for a binary tree node.
224+
* class TreeNode {
225+
* val: number
226+
* left: TreeNode | null
227+
* right: TreeNode | null
228+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
229+
* this.val = (val===undefined ? 0 : val)
230+
* this.left = (left===undefined ? null : left)
231+
* this.right = (right===undefined ? null : right)
232+
* }
233+
* }
234+
*/
235+
236+
function getLonelyNodes(root: TreeNode | null): number[] {
237+
const ans: number[] = [];
238+
const dfs = (root: TreeNode | null) => {
239+
if (!root || root.left === root.right) {
240+
return;
241+
}
242+
if (!root.left) {
243+
ans.push(root.right.val);
244+
}
245+
if (!root.right) {
246+
ans.push(root.left.val);
247+
}
248+
dfs(root.left);
249+
dfs(root.right);
250+
};
251+
dfs(root);
252+
return ans;
203253
}
204254
```
205255

solution/1400-1499/1469.Find All The Lonely Nodes/Solution.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@ class Solution {
1313
public:
1414
vector<int> getLonelyNodes(TreeNode* root) {
1515
vector<int> ans;
16-
function<void(TreeNode * root)> dfs;
17-
dfs = [&](TreeNode* root) {
18-
if (!root || (!root->left && !root->right)) return;
19-
if (!root->left) ans.push_back(root->right->val);
20-
if (!root->right) ans.push_back(root->left->val);
21-
dfs(root->left);
22-
dfs(root->right);
16+
auto dfs = [&](auto&& dfs, TreeNode* root) {
17+
if (!root || (root->left == root->right)) {
18+
return;
19+
}
20+
if (!root->left) {
21+
ans.push_back(root->right->val);
22+
}
23+
if (!root->right) {
24+
ans.push_back(root->left->val);
25+
}
26+
dfs(dfs, root->left);
27+
dfs(dfs, root->right);
2328
};
24-
dfs(root);
29+
dfs(dfs, root);
2530
return ans;
2631
}
27-
};
32+
};

solution/1400-1499/1469.Find All The Lonely Nodes/Solution.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
* Right *TreeNode
77
* }
88
*/
9-
func getLonelyNodes(root *TreeNode) []int {
10-
ans := []int{}
9+
func getLonelyNodes(root *TreeNode) (ans []int) {
1110
var dfs func(*TreeNode)
1211
dfs = func(root *TreeNode) {
13-
if root == nil || (root.Left == nil && root.Right == nil) {
12+
if root == nil || (root.Left == root.Right) {
1413
return
1514
}
1615
if root.Left == nil {
@@ -23,5 +22,5 @@ func getLonelyNodes(root *TreeNode) []int {
2322
dfs(root.Right)
2423
}
2524
dfs(root)
26-
return ans
27-
}
25+
return
26+
}

solution/1400-1499/1469.Find All The Lonely Nodes/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public List<Integer> getLonelyNodes(TreeNode root) {
2222
}
2323

2424
private void dfs(TreeNode root) {
25-
if (root == null || (root.left == null && root.right == null)) {
25+
if (root == null || (root.left == root.right)) {
2626
return;
2727
}
2828
if (root.left == null) {
@@ -34,4 +34,4 @@ private void dfs(TreeNode root) {
3434
dfs(root.left);
3535
dfs(root.right);
3636
}
37-
}
37+
}

solution/1400-1499/1469.Find All The Lonely Nodes/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# self.right = right
77
class Solution:
88
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
9-
def dfs(root):
10-
if root is None or (root.left is None and root.right is None):
9+
def dfs(root: Optional[TreeNode]):
10+
if root is None or root.left == root.right:
1111
return
1212
if root.left is None:
1313
ans.append(root.right.val)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function getLonelyNodes(root: TreeNode | null): number[] {
16+
const ans: number[] = [];
17+
const dfs = (root: TreeNode | null) => {
18+
if (!root || root.left === root.right) {
19+
return;
20+
}
21+
if (!root.left) {
22+
ans.push(root.right.val);
23+
}
24+
if (!root.right) {
25+
ans.push(root.left.val);
26+
}
27+
dfs(root.left);
28+
dfs(root.right);
29+
};
30+
dfs(root);
31+
return ans;
32+
}

0 commit comments

Comments
 (0)