-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path515.java
31 lines (29 loc) · 873 Bytes
/
515.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
import java.util.ArrayList;
import java.util.List;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
List<TreeNode> list = new ArrayList<>();
list.add(root);
while (!list.isEmpty()) {
List<Integer> vals = new ArrayList<>();
List<TreeNode> temp = new ArrayList<>();
for (TreeNode n : list) {
vals.add(n.val);
if (n.left != null) temp.add(n.left);
if (n.right != null) temp.add(n.right);
}
Collections.sort(vals);
res.add(vals.get(vals.size()-1));
list = temp;
}
return res;
}
}