Skip to content

Commit 5a80c21

Browse files
authored
Create 515. Find Largest Value in Each Tree Row1 (#671)
2 parents 18046cf + 9f03fe9 commit 5a80c21

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<int> largestValues(TreeNode* root) {
4+
queue<TreeNode*> q;
5+
if(root==NULL) return {};
6+
q.push(root);
7+
vector<int> res;
8+
while (!q.empty()) {
9+
int n = q.size(), maxi = INT_MIN;
10+
for (int i = 0; i < n; i++) {
11+
TreeNode* node = q.front();
12+
q.pop();
13+
maxi = max(maxi, node->val);
14+
if (node->left)
15+
q.push(node->left);
16+
if (node->right)
17+
q.push(node->right);
18+
}
19+
res.push_back(maxi);
20+
}
21+
return res;
22+
}
23+
};

0 commit comments

Comments
 (0)