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]
這題是BFS典型套路了瞬痘,調(diào)試了幾次過了房揭。注意rowMax = Integer.MIN_VALUE;
不要漏了最欠。
public class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) return result;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int curNum = 1;
int nextNum = 0;
int rowMax = Integer.MIN_VALUE;
while (!queue.isEmpty()) {
TreeNode temp = queue.poll();
curNum--;
if (temp.val > rowMax) rowMax = temp.val;
if (temp.left != null) {
queue.offer(temp.left);
nextNum++;
}
if (temp.right != null) {
queue.offer(temp.right);
nextNum++;
}
if (curNum == 0) {
result.add(rowMax);
curNum = nextNum;
nextNum = 0;
rowMax = Integer.MIN_VALUE;
}
}
return result ;
}
}