-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
48 lines (43 loc) · 1.34 KB
/
Solution.java
File metadata and controls
48 lines (43 loc) · 1.34 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
package leetCode_515;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* @author dimdark
* @date 2017-09-11
* @time 11:38 PM
*/
public class Solution {
public static class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) {
this.val = x;
}
}
public List<Integer> largestValues(TreeNode root) {
List<Integer> rst = new LinkedList<Integer>();
if (root == null) return rst;
Queue<TreeNode> currQueue = new LinkedList<TreeNode>();
Queue<TreeNode> postQueue = new LinkedList<TreeNode>();
Queue<TreeNode> tempQueue;
TreeNode node = root;
currQueue.offer(node);
while (!currQueue.isEmpty()) {
Integer maxValuePerRow = Integer.MIN_VALUE;
while (!currQueue.isEmpty()) {
node = currQueue.peek();
currQueue.poll();
maxValuePerRow = (node.val > maxValuePerRow) ? node.val : maxValuePerRow;
if (node.left != null) postQueue.offer(node.left);
if (node.right != null) postQueue.offer(node.right);
}
rst.add(maxValuePerRow);
// update
tempQueue = currQueue;
currQueue = postQueue;
postQueue = tempQueue;
}
return rst;
}
}