-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1161+Maximum Level Sum of a Binary Tree.cpp
More file actions
43 lines (38 loc) · 1.14 KB
/
Copy path1161+Maximum Level Sum of a Binary Tree.cpp
File metadata and controls
43 lines (38 loc) · 1.14 KB
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
/**
* 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) {}
* };
*/
const int inf = 0x3f3f3f3f;
class Solution {
public:
int maxLevelSum(TreeNode* root) {
vector<pair<int, int>> vecs;
int res = inf;
queue<TreeNode*> q;
q.push(root);
int reslevel = 1, level = 1;
while (q.size()) {
int size = q.size();
int resT = 0;
for (int i = 0; i < size; ++i) {
auto cur = q.front(); q.pop();
resT += cur->val;
if (cur->left) q.push(cur->left);
if (cur->right) q.push(cur->right);
}
if (res < resT) {
res = resT;
reslevel = level;
}
level++;
}
return reslevel;
}
};