File tree Expand file tree Collapse file tree 1 file changed +20
-16
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +20
-16
lines changed Original file line number Diff line number Diff line change 5
5
import java .util .ArrayList ;
6
6
import java .util .List ;
7
7
8
- /**Given a binary tree, collect a tree's nodes as if you were doing this:
8
+ /**
9
+ * 366. Find Leaves of Binary Tree
10
+ *
11
+ * Given a binary tree, collect a tree's nodes as if you were doing this:
9
12
* Collect and remove all leaves, repeat until the tree is empty.
10
13
11
14
Example:
34
37
*/
35
38
public class _366 {
36
39
37
- List <List <Integer >> result = new ArrayList <List <Integer >>();
40
+ public static class Solution1 {
41
+ List <List <Integer >> result = new ArrayList <>();
38
42
39
- public List <List <Integer >> findLeaves (TreeNode root ) {
40
- dfs (root );
41
- return result ;
42
- }
43
-
44
- int dfs (TreeNode root ) {
45
- if (root == null ) {
46
- return 0 ;
43
+ public List <List <Integer >> findLeaves (TreeNode root ) {
44
+ dfs (root );
45
+ return result ;
47
46
}
48
- int level = Math .max (dfs (root .left ), dfs (root .right )) + 1 ;
49
- if (result .size () < level ) {
50
- result .add (new ArrayList <Integer >());
47
+
48
+ int dfs (TreeNode root ) {
49
+ if (root == null ) {
50
+ return 0 ;
51
+ }
52
+ int level = Math .max (dfs (root .left ), dfs (root .right )) + 1 ;
53
+ if (result .size () < level ) {
54
+ result .add (new ArrayList <>());
55
+ }
56
+ result .get (level - 1 ).add (root .val );
57
+ return level ;
51
58
}
52
- result .get (level - 1 ).add (root .val );
53
- return level ;
54
59
}
55
-
56
60
}
You can’t perform that action at this time.
0 commit comments