Given the root
of a binary tree, return an array of the largest value in each row of the tree (0-indexed).
Example 1:
Input: root = [1,3,2,5,3,null,9] Output: [1,3,9]
Example 2:
Input: root = [1,2,3] Output: [1,3]
Example 3:
Input: root = [1] Output: [1]
Example 4:
Input: root = [1,null,2] Output: [1,2]
Example 5:
Input: root = [] Output: []
Constraints:
- The number of nodes in the tree will be in the range
[0, 104]
. -231 <= Node.val <= 231 - 1
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def largestValues(self, root: TreeNode) -> List[int]:
if root is None:
return []
q = deque([root])
ans = []
while q:
n = len(q)
t = float('-inf')
for _ in range(n):
node = q.popleft()
t = max(t, node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
ans.append(t)
return ans
/**
* 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) {
if (root == null) {
return Collections.emptyList();
}
Deque<TreeNode> q = new ArrayDeque<>();
q.offer(root);
List<Integer> ans = new ArrayList<>();
while (!q.isEmpty()) {
int t = Integer.MIN_VALUE;
for (int i = 0, n = q.size(); i < n; ++i) {
TreeNode node = q.poll();
t = Math.max(t, node.val);
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
}
ans.add(t);
}
return ans;
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
if (!root) return {};
queue<TreeNode*> q{{root}};
vector<int> ans;
while (!q.empty())
{
int t = INT_MIN;
for (int i = 0, n = q.size(); i < n; ++i)
{
auto node = q.front();
q.pop();
t = max(t, node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
ans.push_back(t);
}
return ans;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func largestValues(root *TreeNode) []int {
var ans []int
if root == nil {
return ans
}
var q = []*TreeNode{root}
for len(q) > 0 {
n := len(q)
t := math.MinInt32
for i := 0; i < n; i++ {
node := q[0]
q = q[1:]
t = max(t, node.Val)
if node.Left != nil {
q = append(q, node.Left)
}
if node.Right != nil {
q = append(q, node.Right)
}
}
ans = append(ans, t)
}
return ans
}
func max(a, b int) int {
if a > b {
return a
}
return b
}