Find Largest Value in Each Tree Row
ID: 515; medium
Solution 1 (Java)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
Queue<TreeNode> q = new ArrayDeque<>();
q.offer(root);
while (!q.isEmpty()) {
int n = q.size();
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
TreeNode curr = q.poll();
max = Math.max(max, curr.val);
if (curr.left != null)
q.offer(curr.left);
if (curr.right != null)
q.offer(curr.right);
}
res.add(max);
}
return res;
}
}
Notes
BFS
Solution 2 (Java)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
largestValuesHelper(root, 0, res);
return res;
}
private void largestValuesHelper(TreeNode root, int level, List<Integer> res) {
if (root == null) return;
if (level == res.size()) {
res.add(root.val);
} else {
res.set(level, Math.max(res.get(level), root.val));
}
largestValuesHelper(root.left, level + 1, res);
largestValuesHelper(root.right, level + 1, res);
}
}
Notes
DFS
Last updated