-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximumLevelSumofaBinaryTree.cpp
More file actions
40 lines (37 loc) · 1.14 KB
/
maximumLevelSumofaBinaryTree.cpp
File metadata and controls
40 lines (37 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
// Source: https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
// Author: Miao Zhang
// Date: 2021-04-15
/**
* 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) {
vector<int> sums;
function<void(TreeNode*, int)> preorder = [&] (TreeNode* node, int level) {
if (!node) return;
if (sums.size() == level - 1) sums.push_back(0);
sums[level - 1] += node->val;
preorder(node->left, level + 1);
preorder(node->right, level + 1);
};
preorder(root, 1);
int res = 1;
int maxsum = sums[0];
for (int i = 1; i < sums.size(); i++) {
if (sums[i] > maxsum) {
maxsum = sums[i];
res = i + 1;
}
}
return res;
}
};