-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathFindLargestValueInEachTreeRow515.java
More file actions
67 lines (61 loc) · 1.71 KB
/
FindLargestValueInEachTreeRow515.java
File metadata and controls
67 lines (61 loc) · 1.71 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
64
65
66
67
/**
* 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; }
* }
*/
public class FindLargestValueInEachTreeRow515 {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
while (!q.isEmpty()) {
int size = q.size();
int maxVal = Integer.MIN_VALUE;
for (int i=0; i<size; i++) {
TreeNode curr = q.poll();
if (curr.left != null) q.add(curr.left);
if (curr.right != null) q.add(curr.right);
maxVal = Math.max(maxVal, curr.val);
}
res.add(maxVal);
}
return res;
}
/**
* https://leetcode.com/problems/find-largest-value-in-each-tree-row/discuss/98971/9ms-JAVA-DFS-solution
*/
public List<Integer> largestValues2(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
helper(root, res, 0);
return res;
}
private void helper(TreeNode root, List<Integer> res, int d){
if (root == null) return;
if (d == res.size()) { //expand list size
res.add(root.val);
} else{ //or set value
res.set(d, Math.max(res.get(d), root.val));
}
helper(root.left, res, d+1);
helper(root.right, res, d+1);
}
}