Skip to content

Commit 68e0136

Browse files
committed
Implement maximum-level-sum-of-a-binary-tree
https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree time: 224ms time-rank: 79.82% time-complexity: O(N) space: 70.5MB space-rank: 100% space-complexity: O(N)
1 parent 8d027f1 commit 68e0136

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Christopher Friedt
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include <string>
26+
27+
#include <gtest/gtest.h>
28+
29+
#include "util/TreeNode.cpp"
30+
31+
#include "maximum-level-sum-of-a-binary-tree.cpp"
32+
33+
using namespace std;
34+
35+
TEST( MaximumLevelSumOfABinaryTree, Test_empty ) {
36+
TreeNode *root = TreeNode_from_string("[]");
37+
int expected_int = 0;
38+
int actual_int = Solution().maxLevelSum(root);
39+
EXPECT_EQ( actual_int, expected_int );
40+
}
41+
42+
TEST( MaximumLevelSumOfABinaryTree, Test_1_7_0_7_n8_null_null ) {
43+
TreeNode *root = TreeNode_from_string("[1,7,0,7,-8,null,null]");
44+
int expected_int = 2;
45+
int actual_int = Solution().maxLevelSum(root);
46+
EXPECT_EQ( actual_int, expected_int );
47+
}
48+
49+
TEST( MaximumLevelSumOfABinaryTree, Test_989_null_10250_98693_n89388_null_null_null_n32127 ) {
50+
TreeNode *root = TreeNode_from_string("[989,null,10250,98693,-89388,null,null,null,-32127]");
51+
int expected_int = 2;
52+
int actual_int = Solution().maxLevelSum(root);
53+
EXPECT_EQ( actual_int, expected_int );
54+
}
55+
56+
TEST( MaximumLevelSumOfABinaryTree, Test_5_3_2 ) {
57+
TreeNode *root = TreeNode_from_string("[5,3,2]");
58+
int expected_int = 1;
59+
int actual_int = Solution().maxLevelSum(root);
60+
EXPECT_EQ( actual_int, expected_int );
61+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2019 Christopher Friedt
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
/**
26+
* Definition for a binary tree node.
27+
* struct TreeNode {
28+
* int val;
29+
* TreeNode *left;
30+
* TreeNode *right;
31+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
32+
* };
33+
*/
34+
35+
#include <algorithm>
36+
#include <climits>
37+
#include <unordered_map>
38+
#include <vector>
39+
40+
using namespace std;
41+
42+
// https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
43+
44+
class Solution {
45+
public:
46+
int maxLevelSum(TreeNode* root) {
47+
unordered_map<size_t,int> level_sums;
48+
49+
if ( nullptr == root ) {
50+
return 0;
51+
}
52+
53+
calculateSums( level_sums, 1, root );
54+
55+
vector<size_t> levels( level_sums.size(), -1 );
56+
size_t i = 0;
57+
for( auto & kv: level_sums ) {
58+
levels[ i++ ] = kv.first;
59+
}
60+
61+
auto comparator = [&]( const size_t & a, const size_t & b ) -> bool {
62+
if ( level_sums[ b ] > level_sums[ a ] ) {
63+
return false;
64+
}
65+
if ( level_sums[ b ] < level_sums[ a ] ) {
66+
return true;
67+
}
68+
return b > a;
69+
};
70+
71+
sort( levels.begin(), levels.end(), comparator );
72+
73+
return levels[ 0 ];
74+
}
75+
76+
static void calculateSums( unordered_map<size_t,int> & level_sums, const size_t level, const TreeNode *node ) {
77+
if ( nullptr == node ) {
78+
return;
79+
}
80+
81+
calculateSums( level_sums, level + 1, node->left );
82+
calculateSums( level_sums, level + 1, node->right );
83+
level_sums[ level ] += node->val;
84+
}
85+
};

0 commit comments

Comments
 (0)