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]
还是做好行遍历就行了,代码与[Find Bottom Left Tree Value]的结构基本相似,做好每一行的最大值存储即可:
public class FindLargestValueinEachTreeRow {
public static List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null)
return res;
res.add(root.val);
LinkedList<TreeNode> levelNodes = new LinkedList<>();
if (root.left == null && root.right == null) {
return res;
}
levelNodes.add(root);
while (!levelNodes.isEmpty()) {
LinkedList<TreeNode> tempLevel = new LinkedList<>();
for (TreeNode node : levelNodes) {
if (node.left != null)
tempLevel.add(node.left);
if (node.right != null)
tempLevel.add(node.right);
}
if (!tempLevel.isEmpty()) {
int max = (int) (tempLevel.get(0).val);
for (TreeNode node : tempLevel) {
if (node.val > max)
max = node.val;
}
res.add(max);
levelNodes.clear();
levelNodes.addAll(tempLevel);
} else {
break;
}
}
return res;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(3);
root.right = new TreeNode(2);
root.left.left = new TreeNode(5);
root.left.right = new TreeNode(3);
root.right.right = new TreeNode(9);
System.out.println(largestValues(root));
}
}