forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1161.cpp
30 lines (28 loc) · 733 Bytes
/
1161.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
class Solution
{
public:
int maxLevelSum(TreeNode* root)
{
if (!root) return 0;
queue<TreeNode*> q; q.push(root);
int maxSum = 0, res = 0, level = 0;
while (q.size())
{
int curSum = 0, qLen = q.size();
for (int i = 0; i < qLen; ++i)
{
auto node = q.front(); q.pop();
curSum += node->val;
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
level++;
if (curSum > maxSum)
{
maxSum = curSum;
res = level;
}
}
return res;
}
};