-
Notifications
You must be signed in to change notification settings - Fork 21
/
366. Find Leaves of Binary Tree.java
executable file
·84 lines (60 loc) · 1.69 KB
/
366. Find Leaves of Binary Tree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
M
tags: Tree, DFS
time: O(n)
space: O(h)
#### DFS: store nodes at the same depth
- the leaves are at depth 0 and the root is at highest depth
- dfs: the depth = index of the rst, start from depth = 0 at leaf
- end state: leaf node, add to rst, and return depth
```
/*
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.
Example:
Input: [1,2,3,4,5]
1
/ \
2 3
/ \
4 5
Output: [[4,5,3],[2],[1]]
Explanation:
1. Removing the leaves [4,5,3] would result in this tree:
1
/
2
2. Now removing the leaf [2] would result in this tree:
1
3. Now removing the leaf [1] would result in the empty tree:
[]
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/*
- init rst
- dfs: the depth = index of the rst, start from depth = 0 at leaf
- end state: leaf node, add to rst, and return depth
*/
class Solution {
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> rst = new ArrayList<>();
dfs(rst, root);
return rst;
}
private int dfs(List<List<Integer>> rst, TreeNode node) {
if (node == null) return 0;
int leftDepth = dfs(rst, node.left);
int rightDepth = dfs(rst, node.right);
int currDepth = Math.max(leftDepth, rightDepth);
if(rst.size() <= currDepth) rst.add(new ArrayList<>());
rst.get(currDepth).add(node.val);
return currDepth + 1;
}
}
```