Skip to content

Commit c4828ac

Browse files
authored
feat: add solutions to lc problem: No.366 (#3001)
No.0366.Find Leaves of Binary Tree
1 parent 84274cc commit c4828ac

File tree

8 files changed

+391
-256
lines changed

8 files changed

+391
-256
lines changed

solution/0300-0399/0366.Find Leaves of Binary Tree/README.md

Lines changed: 137 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ tags:
5959

6060
<!-- solution:start -->
6161

62-
### 方法一
62+
### 方法一:DFS
63+
64+
我们可以使用深度优先搜索的方法,递归遍历二叉树,将每个节点的高度作为索引,将节点的值添加到对应索引的数组中。
65+
66+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为二叉树的节点个数。
6367

6468
<!-- tabs:start -->
6569

@@ -73,26 +77,20 @@ tags:
7377
# self.left = left
7478
# self.right = right
7579
class Solution:
76-
def findLeaves(self, root: TreeNode) -> List[List[int]]:
77-
def dfs(root, prev, t):
80+
def findLeaves(self, root: Optional[TreeNode]) -> List[List[int]]:
81+
def dfs(root: Optional[TreeNode]) -> int:
7882
if root is None:
79-
return
80-
if root.left is None and root.right is None:
81-
t.append(root.val)
82-
if prev.left == root:
83-
prev.left = None
84-
else:
85-
prev.right = None
86-
dfs(root.left, root, t)
87-
dfs(root.right, root, t)
88-
89-
res = []
90-
prev = TreeNode(left=root)
91-
while prev.left:
92-
t = []
93-
dfs(prev.left, prev, t)
94-
res.append(t)
95-
return res
83+
return 0
84+
l, r = dfs(root.left), dfs(root.right)
85+
h = max(l, r)
86+
if len(ans) == h:
87+
ans.append([])
88+
ans[h].append(root.val)
89+
return h + 1
90+
91+
ans = []
92+
dfs(root)
93+
return ans
9694
```
9795

9896
#### Java
@@ -114,31 +112,25 @@ class Solution:
114112
* }
115113
*/
116114
class Solution {
115+
private List<List<Integer>> ans = new ArrayList<>();
116+
117117
public List<List<Integer>> findLeaves(TreeNode root) {
118-
List<List<Integer>> res = new ArrayList<>();
119-
TreeNode prev = new TreeNode(0, root, null);
120-
while (prev.left != null) {
121-
List<Integer> t = new ArrayList<>();
122-
dfs(prev.left, prev, t);
123-
res.add(t);
124-
}
125-
return res;
118+
dfs(root);
119+
return ans;
126120
}
127121

128-
private void dfs(TreeNode root, TreeNode prev, List<Integer> t) {
122+
private int dfs(TreeNode root) {
129123
if (root == null) {
130-
return;
124+
return 0;
131125
}
132-
if (root.left == null && root.right == null) {
133-
t.add(root.val);
134-
if (prev.left == root) {
135-
prev.left = null;
136-
} else {
137-
prev.right = null;
138-
}
126+
int l = dfs(root.left);
127+
int r = dfs(root.right);
128+
int h = Math.max(l, r);
129+
if (ans.size() == h) {
130+
ans.add(new ArrayList<>());
139131
}
140-
dfs(root.left, root, t);
141-
dfs(root.right, root, t);
132+
ans.get(h).add(root.val);
133+
return h + 1;
142134
}
143135
}
144136
```
@@ -160,27 +152,22 @@ class Solution {
160152
class Solution {
161153
public:
162154
vector<vector<int>> findLeaves(TreeNode* root) {
163-
vector<vector<int>> res;
164-
TreeNode* prev = new TreeNode(0, root, nullptr);
165-
while (prev->left) {
166-
vector<int> t;
167-
dfs(prev->left, prev, t);
168-
res.push_back(t);
169-
}
170-
return res;
171-
}
172-
173-
void dfs(TreeNode* root, TreeNode* prev, vector<int>& t) {
174-
if (!root) return;
175-
if (!root->left && !root->right) {
176-
t.push_back(root->val);
177-
if (prev->left == root)
178-
prev->left = nullptr;
179-
else
180-
prev->right = nullptr;
181-
}
182-
dfs(root->left, root, t);
183-
dfs(root->right, root, t);
155+
vector<vector<int>> ans;
156+
function<int(TreeNode*)> dfs = [&](TreeNode* root) {
157+
if (!root) {
158+
return 0;
159+
}
160+
int l = dfs(root->left);
161+
int r = dfs(root->right);
162+
int h = max(l, r);
163+
if (ans.size() == h) {
164+
ans.push_back({});
165+
}
166+
ans[h].push_back(root->val);
167+
return h + 1;
168+
};
169+
dfs(root);
170+
return ans;
184171
}
185172
};
186173
```
@@ -196,35 +183,99 @@ public:
196183
* Right *TreeNode
197184
* }
198185
*/
199-
func findLeaves(root *TreeNode) [][]int {
200-
prev := &TreeNode{
201-
Val: 0,
202-
Left: root,
203-
Right: nil,
204-
}
205-
var res [][]int
206-
for prev.Left != nil {
207-
var t []int
208-
dfs(prev.Left, prev, &t)
209-
res = append(res, t)
186+
func findLeaves(root *TreeNode) (ans [][]int) {
187+
var dfs func(*TreeNode) int
188+
dfs = func(root *TreeNode) int {
189+
if root == nil {
190+
return 0
191+
}
192+
l, r := dfs(root.Left), dfs(root.Right)
193+
h := max(l, r)
194+
if len(ans) == h {
195+
ans = append(ans, []int{})
196+
}
197+
ans[h] = append(ans[h], root.Val)
198+
return h + 1
210199
}
211-
return res
200+
dfs(root)
201+
return
212202
}
203+
```
213204

214-
func dfs(root, prev *TreeNode, t *[]int) {
215-
if root == nil {
216-
return
217-
}
218-
if root.Left == nil && root.Right == nil {
219-
*t = append(*t, root.Val)
220-
if prev.Left == root {
221-
prev.Left = nil
222-
} else {
223-
prev.Right = nil
224-
}
225-
}
226-
dfs(root.Left, root, t)
227-
dfs(root.Right, root, t)
205+
#### TypeScript
206+
207+
```ts
208+
/**
209+
* Definition for a binary tree node.
210+
* class TreeNode {
211+
* val: number
212+
* left: TreeNode | null
213+
* right: TreeNode | null
214+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
215+
* this.val = (val===undefined ? 0 : val)
216+
* this.left = (left===undefined ? null : left)
217+
* this.right = (right===undefined ? null : right)
218+
* }
219+
* }
220+
*/
221+
222+
function findLeaves(root: TreeNode | null): number[][] {
223+
const ans: number[][] = [];
224+
const dfs = (root: TreeNode | null): number => {
225+
if (root === null) {
226+
return 0;
227+
}
228+
const l = dfs(root.left);
229+
const r = dfs(root.right);
230+
const h = Math.max(l, r);
231+
if (ans.length === h) {
232+
ans.push([]);
233+
}
234+
ans[h].push(root.val);
235+
return h + 1;
236+
};
237+
dfs(root);
238+
return ans;
239+
}
240+
```
241+
242+
#### C#
243+
244+
```cs
245+
/**
246+
* Definition for a binary tree node.
247+
* public class TreeNode {
248+
* public int val;
249+
* public TreeNode left;
250+
* public TreeNode right;
251+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
252+
* this.val = val;
253+
* this.left = left;
254+
* this.right = right;
255+
* }
256+
* }
257+
*/
258+
public class Solution {
259+
public IList<IList<int>> FindLeaves(TreeNode root) {
260+
var ans = new List<IList<int>>();
261+
262+
int Dfs(TreeNode node) {
263+
if (node == null) {
264+
return 0;
265+
}
266+
int l = Dfs(node.left);
267+
int r = Dfs(node.right);
268+
int h = Math.Max(l, r);
269+
if (ans.Count == h) {
270+
ans.Add(new List<int>());
271+
}
272+
ans[h].Add(node.val);
273+
return h + 1;
274+
}
275+
276+
Dfs(root);
277+
return ans;
278+
}
228279
}
229280
```
230281

0 commit comments

Comments
 (0)