We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 18046cf commit 9f03fe9Copy full SHA for 9f03fe9
515. Find Largest Value in Each Tree Row1
@@ -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