Find Largest Value in Each Tree Row LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> res;
4
vector<int> largestValues(TreeNode *root) {
5
if (!root) return {};
6
if (!root->left && !root->right) return {root->val};
7
TreeNode *temp;
8
int mx = INT_MIN;
9
queue<TreeNode *> q;
10
q.push(root);
11

12
while (!q.empty()) {
13
int sz = q.size();
14
while (sz--) {
15
temp = q.front();
16
if (temp->val > mx) mx = temp->val;
17
q.pop();
18
if (temp->left) q.push(temp->left);
19
if (temp->right) q.push(temp->right);
20
}
21
res.push_back(mx);
22
mx = INT_MIN;
23
}
24
return res;
25
}
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0