Skip to content

Commit a7c0a41

Browse files
committed
feat: add solutions to lc problem: No.1469
No.1469.Find All The Lonely Nodes
1 parent 80947bb commit a7c0a41

File tree

6 files changed

+259
-74
lines changed

6 files changed

+259
-74
lines changed

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

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@
7272

7373
<!-- 这里可写通用的实现逻辑 -->
7474

75+
**方法一:递归**
76+
77+
递归搜索二叉树,如果当前节点的左右子节点都不为空,则继续递归搜索左右子树;如果当前节点的左右子节点有一个为空,则将不为空的子节点的值加入结果数组中,然后继续递归搜索左右子树。
78+
79+
时间复杂度 $O(n)$,其中 $n$ 为二叉树的节点个数。需要对二叉树进行一次遍历。
80+
7581
<!-- tabs:start -->
7682

7783
### **Python3**
@@ -86,20 +92,20 @@
8692
# self.left = left
8793
# self.right = right
8894
class Solution:
89-
def getLonelyNodes(self, root: TreeNode) -> List[int]:
90-
def traverse(root):
91-
if root is None:
95+
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):
9298
return
93-
if root.left is None and root.right is not None:
94-
self.res.append(root.right.val)
95-
if root.left is not None and root.right is None:
96-
self.res.append(root.left.val)
97-
traverse(root.left)
98-
traverse(root.right)
99-
100-
self.res = []
101-
traverse(root)
102-
return self.res
99+
if root.left is None:
100+
ans.append(root.right.val)
101+
if root.right is None:
102+
ans.append(root.left.val)
103+
dfs(root.left)
104+
dfs(root.right)
105+
106+
ans = []
107+
dfs(root)
108+
return ans
103109
```
104110

105111
### **Java**
@@ -123,27 +129,90 @@ class Solution:
123129
* }
124130
*/
125131
class Solution {
126-
private List<Integer> res;
132+
private List<Integer> ans = new ArrayList<>();
127133

128134
public List<Integer> getLonelyNodes(TreeNode root) {
129-
res = new ArrayList<>();
130-
traverse(root);
131-
return res;
135+
dfs(root);
136+
return ans;
132137
}
133138

134-
private void traverse(TreeNode root) {
135-
if (root == null) {
139+
private void dfs(TreeNode root) {
140+
if (root == null || (root.left == null && root.right == null)) {
136141
return;
137142
}
138-
if (root.left == null && root.right != null) {
139-
res.add(root.right.val);
143+
if (root.left == null) {
144+
ans.add(root.right.val);
140145
}
141-
if (root.left != null && root.right == null) {
142-
res.add(root.left.val);
146+
if (root.right == null) {
147+
ans.add(root.left.val);
143148
}
144-
traverse(root.left);
145-
traverse(root.right);
149+
dfs(root.left);
150+
dfs(root.right);
151+
}
152+
}
153+
```
154+
155+
### **C++**
156+
157+
```cpp
158+
/**
159+
* Definition for a binary tree node.
160+
* struct TreeNode {
161+
* int val;
162+
* TreeNode *left;
163+
* TreeNode *right;
164+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
165+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
166+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
167+
* };
168+
*/
169+
class Solution {
170+
public:
171+
vector<int> getLonelyNodes(TreeNode* root) {
172+
vector<int> ans;
173+
function<void(TreeNode* root)> dfs;
174+
dfs = [&](TreeNode* root) {
175+
if (!root || (!root->left && !root->right)) return;
176+
if (!root->left) ans.push_back(root->right->val);
177+
if (!root->right) ans.push_back(root->left->val);
178+
dfs(root->left);
179+
dfs(root->right);
180+
};
181+
dfs(root);
182+
return ans;
146183
}
184+
};
185+
```
186+
187+
### **Go**
188+
189+
```go
190+
/**
191+
* Definition for a binary tree node.
192+
* type TreeNode struct {
193+
* Val int
194+
* Left *TreeNode
195+
* Right *TreeNode
196+
* }
197+
*/
198+
func getLonelyNodes(root *TreeNode) []int {
199+
ans := []int{}
200+
var dfs func(*TreeNode)
201+
dfs = func(root *TreeNode) {
202+
if root == nil || (root.Left == nil && root.Right == nil) {
203+
return
204+
}
205+
if root.Left == nil {
206+
ans = append(ans, root.Right.Val)
207+
}
208+
if root.Right == nil {
209+
ans = append(ans, root.Left.Val)
210+
}
211+
dfs(root.Left)
212+
dfs(root.Right)
213+
}
214+
dfs(root)
215+
return ans
147216
}
148217
```
149218

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

Lines changed: 88 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,20 @@ All other nodes are lonely.
6060
# self.left = left
6161
# self.right = right
6262
class Solution:
63-
def getLonelyNodes(self, root: TreeNode) -> List[int]:
64-
def traverse(root):
65-
if root is None:
63+
def getLonelyNodes(self, root: Optional[TreeNode]) -> List[int]:
64+
def dfs(root):
65+
if root is None or (root.left is None and root.right is None):
6666
return
67-
if root.left is None and root.right is not None:
68-
self.res.append(root.right.val)
69-
if root.left is not None and root.right is None:
70-
self.res.append(root.left.val)
71-
traverse(root.left)
72-
traverse(root.right)
73-
74-
self.res = []
75-
traverse(root)
76-
return self.res
67+
if root.left is None:
68+
ans.append(root.right.val)
69+
if root.right is None:
70+
ans.append(root.left.val)
71+
dfs(root.left)
72+
dfs(root.right)
73+
74+
ans = []
75+
dfs(root)
76+
return ans
7777
```
7878

7979
### **Java**
@@ -95,30 +95,93 @@ class Solution:
9595
* }
9696
*/
9797
class Solution {
98-
private List<Integer> res;
98+
private List<Integer> ans = new ArrayList<>();
9999

100100
public List<Integer> getLonelyNodes(TreeNode root) {
101-
res = new ArrayList<>();
102-
traverse(root);
103-
return res;
101+
dfs(root);
102+
return ans;
104103
}
105104

106-
private void traverse(TreeNode root) {
107-
if (root == null) {
105+
private void dfs(TreeNode root) {
106+
if (root == null || (root.left == null && root.right == null)) {
108107
return;
109108
}
110-
if (root.left == null && root.right != null) {
111-
res.add(root.right.val);
109+
if (root.left == null) {
110+
ans.add(root.right.val);
112111
}
113-
if (root.left != null && root.right == null) {
114-
res.add(root.left.val);
112+
if (root.right == null) {
113+
ans.add(root.left.val);
115114
}
116-
traverse(root.left);
117-
traverse(root.right);
115+
dfs(root.left);
116+
dfs(root.right);
118117
}
119118
}
120119
```
121120

121+
### **C++**
122+
123+
```cpp
124+
/**
125+
* Definition for a binary tree node.
126+
* struct TreeNode {
127+
* int val;
128+
* TreeNode *left;
129+
* TreeNode *right;
130+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
131+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
132+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
133+
* };
134+
*/
135+
class Solution {
136+
public:
137+
vector<int> getLonelyNodes(TreeNode* root) {
138+
vector<int> ans;
139+
function<void(TreeNode* root)> dfs;
140+
dfs = [&](TreeNode* root) {
141+
if (!root || (!root->left && !root->right)) return;
142+
if (!root->left) ans.push_back(root->right->val);
143+
if (!root->right) ans.push_back(root->left->val);
144+
dfs(root->left);
145+
dfs(root->right);
146+
};
147+
dfs(root);
148+
return ans;
149+
}
150+
};
151+
```
152+
153+
### **Go**
154+
155+
```go
156+
/**
157+
* Definition for a binary tree node.
158+
* type TreeNode struct {
159+
* Val int
160+
* Left *TreeNode
161+
* Right *TreeNode
162+
* }
163+
*/
164+
func getLonelyNodes(root *TreeNode) []int {
165+
ans := []int{}
166+
var dfs func(*TreeNode)
167+
dfs = func(root *TreeNode) {
168+
if root == nil || (root.Left == nil && root.Right == nil) {
169+
return
170+
}
171+
if root.Left == nil {
172+
ans = append(ans, root.Right.Val)
173+
}
174+
if root.Right == nil {
175+
ans = append(ans, root.Left.Val)
176+
}
177+
dfs(root.Left)
178+
dfs(root.Right)
179+
}
180+
dfs(root)
181+
return ans
182+
}
183+
```
184+
122185
### **...**
123186

124187
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
vector<int> getLonelyNodes(TreeNode* root) {
15+
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);
23+
};
24+
dfs(root);
25+
return ans;
26+
}
27+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func getLonelyNodes(root *TreeNode) []int {
10+
ans := []int{}
11+
var dfs func(*TreeNode)
12+
dfs = func(root *TreeNode) {
13+
if root == nil || (root.Left == nil && root.Right == nil) {
14+
return
15+
}
16+
if root.Left == nil {
17+
ans = append(ans, root.Right.Val)
18+
}
19+
if root.Right == nil {
20+
ans = append(ans, root.Left.Val)
21+
}
22+
dfs(root.Left)
23+
dfs(root.Right)
24+
}
25+
dfs(root)
26+
return ans
27+
}

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,24 @@
1414
* }
1515
*/
1616
class Solution {
17-
private List<Integer> res;
17+
private List<Integer> ans = new ArrayList<>();
1818

1919
public List<Integer> getLonelyNodes(TreeNode root) {
20-
res = new ArrayList<>();
21-
traverse(root);
22-
return res;
20+
dfs(root);
21+
return ans;
2322
}
2423

25-
private void traverse(TreeNode root) {
26-
if (root == null) {
24+
private void dfs(TreeNode root) {
25+
if (root == null || (root.left == null && root.right == null)) {
2726
return;
2827
}
29-
if (root.left == null && root.right != null) {
30-
res.add(root.right.val);
28+
if (root.left == null) {
29+
ans.add(root.right.val);
3130
}
32-
if (root.left != null && root.right == null) {
33-
res.add(root.left.val);
31+
if (root.right == null) {
32+
ans.add(root.left.val);
3433
}
35-
traverse(root.left);
36-
traverse(root.right);
34+
dfs(root.left);
35+
dfs(root.right);
3736
}
3837
}

0 commit comments

Comments
 (0)