-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_1161_MaxLevelSumOfBT.cpp
44 lines (41 loc) · 1.1 KB
/
LC_1161_MaxLevelSumOfBT.cpp
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
37
38
39
40
41
42
43
44
/*
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
1161. Maximum Level Sum of a Binary Tree
*/
/**
* 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:
int maxLevelSum(TreeNode* root) {
int l=0, mxsum = INT_MIN, level=0;
queue<TreeNode*> q; q.push(root);
while(!q.empty())
{
int sz = q.size();
l++; // current level
int sum=0;
while(sz--)
{
TreeNode *t = q.front(); q.pop();
sum += t->val;
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
if(sum>mxsum)
{
mxsum = sum;
level = l;
}
}
return level;
}
};