-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-515-Find-Largest-Value-in-Each-Tree-Row.java
More file actions
63 lines (53 loc) · 2.02 KB
/
LeetCode-515-Find-Largest-Value-in-Each-Tree-Row.java
File metadata and controls
63 lines (53 loc) · 2.02 KB
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
// 1.BFS. Level order traversal
// public List<Integer> largestValues(TreeNode root) {
// List<Integer> result = new ArrayList<>();
// if (root == null) return result;
// Queue<TreeNode> queue = new LinkedList<>();
// queue.add(root);
// while(!queue.isEmpty()) {
// // proceed each level
// int size = queue.size();
// int largest = Integer.MIN_VALUE;
// for (int i = 0; i < size; i++) {
// TreeNode node = queue.poll();
// if (node.val > largest) largest = node.val;
// if (node.left != null) queue.add(node.left);
// if (node.right != null) queue.add(node.right);
// }
// result.add(largest);
// }
// return result;
// }
// 2.DFS. Add a level as the index in the result list.
public List<Integer> largestValues(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
dfs(root, 0, result);
return result;
}
private void dfs(TreeNode root, int level, List<Integer> result) {
// root should not be null
if (level == result.size()) {
// the first node in this level, just add the root.val
result.add(root.val);
} else {
// not the first node in this level, update the root.val, if it's larger than the curr value in the result in this level
if (root.val > result.get(level)) {
result.set(level, root.val);
}
}
// continue traversal
if (root.left != null) dfs(root.left, level + 1, result);
if (root.right != null) dfs(root.right, level + 1, result);
}
}