515. Find Largest Value in Each Tree Row

515. Find Largest Value in Each Tree Row

Description

Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed) .

Example 1:

1
2
Input: root = [1,3,2,5,3,null,9]
Output: [1,3,9]

Example 2:

1
2
Input: root = [1,2,3]
Output: [1,3]

Constraints:

  • The number of nodes in the tree will be in the range [0, 10^4].
  • -2^31 <= Node.val <= 2^31 - 1

Hints/Notes

  • binary tree

Solution

Language: C++

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
/**
* 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) {
vector<int> res;
queue<TreeNode*> q;
if (root)
q.push(root);
while (!q.empty()) {
int maxValue = INT_MIN;
int n = q.size();
for (int i = 0; i < n; i++) {
TreeNode* cur = q.front();
q.pop();
maxValue = max(maxValue, cur->val);
if (cur->left)
q.push(cur->left);
if (cur->right)
q.push(cur->right);
}
res.push_back(maxValue);
}
return res;
}
};