Skip to content

Commit 082f098

Browse files
committed
feat: add solutions to lc problem: No.1161
No.1161.Maximum Level Sum of a Binary Tree
1 parent a79ecfa commit 082f098

File tree

6 files changed

+436
-2
lines changed

6 files changed

+436
-2
lines changed

solution/1100-1199/1161.Maximum Level Sum of a Binary Tree/README.md

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,163 @@
5151
<!-- 这里可写当前语言的特殊实现逻辑 -->
5252

5353
```python
54-
54+
# Definition for a binary tree node.
55+
# class TreeNode:
56+
# def __init__(self, val=0, left=None, right=None):
57+
# self.val = val
58+
# self.left = left
59+
# self.right = right
60+
class Solution:
61+
def maxLevelSum(self, root: TreeNode) -> int:
62+
ans = (float('-inf'), 0)
63+
q = deque([root])
64+
l = 0
65+
while q:
66+
l += 1
67+
n = len(q)
68+
s = 0
69+
for _ in range(n):
70+
node = q.popleft()
71+
s += node.val
72+
if node.left:
73+
q.append(node.left)
74+
if node.right:
75+
q.append(node.right)
76+
if s > ans[0]:
77+
ans = (s, l)
78+
return ans[1]
5579
```
5680

5781
### **Java**
5882

5983
<!-- 这里可写当前语言的特殊实现逻辑 -->
6084

6185
```java
86+
/**
87+
* Definition for a binary tree node.
88+
* public class TreeNode {
89+
* int val;
90+
* TreeNode left;
91+
* TreeNode right;
92+
* TreeNode() {}
93+
* TreeNode(int val) { this.val = val; }
94+
* TreeNode(int val, TreeNode left, TreeNode right) {
95+
* this.val = val;
96+
* this.left = left;
97+
* this.right = right;
98+
* }
99+
* }
100+
*/
101+
class Solution {
102+
103+
public int maxLevelSum(TreeNode root) {
104+
int[] ans = new int[] { Integer.MIN_VALUE, 0 };
105+
int l = 0;
106+
Deque<TreeNode> q = new LinkedList<>();
107+
q.offerLast(root);
108+
while (!q.isEmpty()) {
109+
++l;
110+
int s = 0;
111+
for (int i = q.size(); i > 0; --i) {
112+
TreeNode node = q.pollFirst();
113+
s += node.val;
114+
if (node.left != null) {
115+
q.offerLast(node.left);
116+
}
117+
if (node.right != null) {
118+
q.offerLast(node.right);
119+
}
120+
}
121+
if (s > ans[0]) {
122+
ans[0] = s;
123+
ans[1] = l;
124+
}
125+
}
126+
return ans[1];
127+
}
128+
}
129+
130+
```
131+
132+
### **C++**
133+
134+
```cpp
135+
/**
136+
* Definition for a binary tree node.
137+
* struct TreeNode {
138+
* int val;
139+
* TreeNode *left;
140+
* TreeNode *right;
141+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
142+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
143+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
144+
* };
145+
*/
146+
class Solution {
147+
public:
148+
int maxLevelSum(TreeNode* root) {
149+
vector<int> ans(2);
150+
ans[0] = INT_MIN;
151+
queue<TreeNode*> q{{root}};
152+
int l = 0;
153+
while (!q.empty())
154+
{
155+
++l;
156+
int s = 0;
157+
for (int i = q.size(); i > 0; --i)
158+
{
159+
TreeNode* node = q.front();
160+
q.pop();
161+
s += node->val;
162+
if (node->left) q.push(node->left);
163+
if (node->right) q.push(node->right);
164+
}
165+
if (s > ans[0])
166+
{
167+
ans[0] = s;
168+
ans[1] = l;
169+
}
170+
}
171+
return ans[1];
172+
}
173+
};
174+
```
62175
176+
### **Go**
177+
178+
```go
179+
/**
180+
* Definition for a binary tree node.
181+
* type TreeNode struct {
182+
* Val int
183+
* Left *TreeNode
184+
* Right *TreeNode
185+
* }
186+
*/
187+
func maxLevelSum(root *TreeNode) int {
188+
ans := [2]int{math.MinInt32, 0}
189+
q := []*TreeNode{root}
190+
l := 0
191+
for len(q) > 0 {
192+
l++
193+
s := 0
194+
for i := len(q); i > 0; i-- {
195+
node := q[0]
196+
q = q[1:]
197+
s += node.Val
198+
if node.Left != nil {
199+
q = append(q, node.Left)
200+
}
201+
if node.Right != nil {
202+
q = append(q, node.Right)
203+
}
204+
}
205+
if s > ans[0] {
206+
ans = [2]int{s, l}
207+
}
208+
}
209+
return ans[1]
210+
}
63211
```
64212

65213
### **...**

solution/1100-1199/1161.Maximum Level Sum of a Binary Tree/README_EN.md

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,161 @@ So we return the level with the maximum sum which is level 2.
4343
### **Python3**
4444

4545
```python
46-
46+
# Definition for a binary tree node.
47+
# class TreeNode:
48+
# def __init__(self, val=0, left=None, right=None):
49+
# self.val = val
50+
# self.left = left
51+
# self.right = right
52+
class Solution:
53+
def maxLevelSum(self, root: TreeNode) -> int:
54+
ans = (float('-inf'), 0)
55+
q = deque([root])
56+
l = 0
57+
while q:
58+
l += 1
59+
n = len(q)
60+
s = 0
61+
for _ in range(n):
62+
node = q.popleft()
63+
s += node.val
64+
if node.left:
65+
q.append(node.left)
66+
if node.right:
67+
q.append(node.right)
68+
if s > ans[0]:
69+
ans = (s, l)
70+
return ans[1]
4771
```
4872

4973
### **Java**
5074

5175
```java
76+
/**
77+
* Definition for a binary tree node.
78+
* public class TreeNode {
79+
* int val;
80+
* TreeNode left;
81+
* TreeNode right;
82+
* TreeNode() {}
83+
* TreeNode(int val) { this.val = val; }
84+
* TreeNode(int val, TreeNode left, TreeNode right) {
85+
* this.val = val;
86+
* this.left = left;
87+
* this.right = right;
88+
* }
89+
* }
90+
*/
91+
class Solution {
92+
93+
public int maxLevelSum(TreeNode root) {
94+
int[] ans = new int[] { Integer.MIN_VALUE, 0 };
95+
int l = 0;
96+
Deque<TreeNode> q = new LinkedList<>();
97+
q.offerLast(root);
98+
while (!q.isEmpty()) {
99+
++l;
100+
int s = 0;
101+
for (int i = q.size(); i > 0; --i) {
102+
TreeNode node = q.pollFirst();
103+
s += node.val;
104+
if (node.left != null) {
105+
q.offerLast(node.left);
106+
}
107+
if (node.right != null) {
108+
q.offerLast(node.right);
109+
}
110+
}
111+
if (s > ans[0]) {
112+
ans[0] = s;
113+
ans[1] = l;
114+
}
115+
}
116+
return ans[1];
117+
}
118+
}
119+
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
/**
126+
* Definition for a binary tree node.
127+
* struct TreeNode {
128+
* int val;
129+
* TreeNode *left;
130+
* TreeNode *right;
131+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
132+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
133+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
134+
* };
135+
*/
136+
class Solution {
137+
public:
138+
int maxLevelSum(TreeNode* root) {
139+
vector<int> ans(2);
140+
ans[0] = INT_MIN;
141+
queue<TreeNode*> q{{root}};
142+
int l = 0;
143+
while (!q.empty())
144+
{
145+
++l;
146+
int s = 0;
147+
for (int i = q.size(); i > 0; --i)
148+
{
149+
TreeNode* node = q.front();
150+
q.pop();
151+
s += node->val;
152+
if (node->left) q.push(node->left);
153+
if (node->right) q.push(node->right);
154+
}
155+
if (s > ans[0])
156+
{
157+
ans[0] = s;
158+
ans[1] = l;
159+
}
160+
}
161+
return ans[1];
162+
}
163+
};
164+
```
52165
166+
### **Go**
167+
168+
```go
169+
/**
170+
* Definition for a binary tree node.
171+
* type TreeNode struct {
172+
* Val int
173+
* Left *TreeNode
174+
* Right *TreeNode
175+
* }
176+
*/
177+
func maxLevelSum(root *TreeNode) int {
178+
ans := [2]int{math.MinInt32, 0}
179+
q := []*TreeNode{root}
180+
l := 0
181+
for len(q) > 0 {
182+
l++
183+
s := 0
184+
for i := len(q); i > 0; i-- {
185+
node := q[0]
186+
q = q[1:]
187+
s += node.Val
188+
if node.Left != nil {
189+
q = append(q, node.Left)
190+
}
191+
if node.Right != nil {
192+
q = append(q, node.Right)
193+
}
194+
}
195+
if s > ans[0] {
196+
ans = [2]int{s, l}
197+
}
198+
}
199+
return ans[1]
200+
}
53201
```
54202

55203
### **...**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int maxLevelSum(TreeNode* root) {
15+
vector<int> ans(2);
16+
ans[0] = INT_MIN;
17+
queue<TreeNode*> q{{root}};
18+
int l = 0;
19+
while (!q.empty())
20+
{
21+
++l;
22+
int s = 0;
23+
for (int i = q.size(); i > 0; --i)
24+
{
25+
TreeNode* node = q.front();
26+
q.pop();
27+
s += node->val;
28+
if (node->left) q.push(node->left);
29+
if (node->right) q.push(node->right);
30+
}
31+
if (s > ans[0])
32+
{
33+
ans[0] = s;
34+
ans[1] = l;
35+
}
36+
}
37+
return ans[1];
38+
}
39+
};

0 commit comments

Comments
 (0)