forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_366.java
30 lines (24 loc) · 746 Bytes
/
_366.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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class _366 {
public static class Solution1 {
List<List<Integer>> result = new ArrayList<>();
public List<List<Integer>> findLeaves(TreeNode root) {
dfs(root);
return result;
}
int dfs(TreeNode root) {
if (root == null) {
return 0;
}
int level = Math.max(dfs(root.left), dfs(root.right)) + 1;
if (result.size() < level) {
result.add(new ArrayList<>());
}
result.get(level - 1).add(root.val);
return level;
}
}
}