8000 refactor 366 · Mars2018/Leetcode-3@885c63c · GitHub
[go: up one dir, main page]

Skip to content

Commit 885c63c

Browse files
refactor 366
1 parent 86d1d47 commit 885c63c

File tree

1 file changed

+20
-16
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+20
-16
lines changed

src/main/java/com/fishercoder/solutions/_366.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

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:
912
* Collect and remove all leaves, repeat until the tree is empty.
1013
1114
Example:
@@ -34,23 +37,24 @@
3437
*/
3538
public class _366 {
3639

37-
List<List<Integer>> result = new ArrayList<List<Integer>>();
40+
public static class Solution1 {
41+
List<List<Integer>> result = new ArrayList<>();
3842

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;
4746
}
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;
5158
}
52-
result.get(level - 1).add(root.val);
53-
return level;
5459
}
55-
5660
}

0 commit comments

Comments
 (0)
0