-
Notifications
You must be signed in to change notification settings - Fork 23
/
515. Find Largest Value in Each Tree Row.java
executable file
·88 lines (75 loc) · 1.89 KB
/
515. Find Largest Value in Each Tree Row.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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
M
tags: Tree, BFS, DFS
time: O(n)
space: O(n)
#### Method1: DFS
- faster than BFS, using less space if not couting final rst: stack size, O(logn)
- time: O(n), visit all
#### Method2: BFS with queue
- loop over queue level and record max
```
/*
You need to find the largest value in each row of a binary tree.
Example:
Input:
1
/ \
3 2
/ \ \
5 3 9
Output: [1, 3, 9]
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/*
DFS, use level to track: 1) time to add new item, 2) index to insert max val
time: O(n)
space: O(n)
*/
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> rst = new ArrayList<>();
dfs(rst, root, 0);
return rst;
}
private void dfs(List<Integer> rst, TreeNode node, int level) {
if (node == null) return;
if (level == rst.size()) rst.add(node.val);
else rst.set(level, Math.max(rst.get(level), node.val));
dfs(rst, node.left, level + 1);
dfs(rst, node.right, level + 1);
}
}
/*
BFS:
time: O(n)
space: O(n)
*/
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> rst = new ArrayList<>();
if (root == null) return rst;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
int max = queue.peek().val;
while (size-- > 0) {
TreeNode node = queue.poll();
max = Math.max(max, node.val);
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
rst.add(max);
}
return rst;
}
}
```