Skip to content

Commit 79cf727

Browse files
chore: add LeetCode daily solution
1 parent 00bd0f1 commit 79cf727

5 files changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Maximum Level Sum of a Binary Tree (Medium)
2+
3+
**Problem ID:** 1161
4+
**Date:** 2026-01-06
5+
**Link:** https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
6+
7+
## Approach
8+
9+
To solve the problem of finding the smallest level in a binary tree with the maximum sum of node values, we can utilize a breadth-first search (BFS) approach. Here’s a concise breakdown of the approach:
10+
11+
### Main Idea:
12+
The goal is to traverse the binary tree level by level, calculate the sum of node values at each level, and keep track of the maximum sum encountered. We also need to ensure that if multiple levels have the same maximum sum, we return the smallest level.
13+
14+
### Approach:
15+
1. **BFS Traversal**: Use a queue to perform a level-order traversal of the binary tree. This allows us to process each level of the tree one at a time.
16+
17+
2. **Level Sum Calculation**: For each level, initialize a sum variable to zero. As we dequeue nodes from the queue, add their values to this sum. Also, enqueue their children (if they exist) for processing in the next level.
18+
19+
3. **Tracking Maximum Sum**: Maintain variables to track the maximum sum found (`max_sum`) and the corresponding level (`max_level`). After processing each level, compare the current level's sum with `max_sum`. If the current sum exceeds `max_sum`, update both `max_sum` and `max_level`. If it equals `max_sum`, do not update `max_level` since we want the smallest level.
20+
21+
4. **Return Result**: Once all levels are processed, return `max_level`, which will hold the smallest level with the maximum sum.
22+
23+
### Data Structures:
24+
- A queue (FIFO) to facilitate the level-order traversal.
25+
- Variables to store the current level sum, maximum sum, and the level corresponding to the maximum sum.
26+
27+
### Complexity:
28+
- **Time Complexity**: O(N), where N is the number of nodes in the tree. Each node is processed exactly once.
29+
- **Space Complexity**: O(W), where W is the maximum width of the tree. In the worst case, this is O(N) for a completely unbalanced tree, but typically it will be less.
30+
31+
This approach efficiently computes the desired level while ensuring clarity and simplicity in the traversal and summation process.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
public class Solution {
5+
public int maxLevelSum(TreeNode root) {
6+
if (root == null) return 0;
7+
8+
int maxSum = Integer.MIN_VALUE;
9+
int maxLevel = 1;
10+
int currentLevel = 1;
11+
12+
Queue<TreeNode> queue = new LinkedList<>();
13+
queue.offer(root);
14+
15+
while (!queue.isEmpty()) {
16+
int levelSize = queue.size();
17+
int currentSum = 0;
18+
19+
for (int i = 0; i < levelSize; i++) {
20+
TreeNode node = queue.poll();
21+
currentSum += node.val;
22+
23+
if (node.left != null) queue.offer(node.left);
24+
if (node.right != null) queue.offer(node.right);
25+
}
26+
27+
if (currentSum > maxSum) {
28+
maxSum = currentSum;
29+
maxLevel = currentLevel;
30+
}
31+
32+
currentLevel++;
33+
}
34+
35+
return maxLevel;
36+
}
37+
}
38+
39+
class TreeNode {
40+
int val;
41+
TreeNode left;
42+
TreeNode right;
43+
TreeNode(int x) { val = x; }
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class TreeNode {
2+
constructor(val, left = null, right = null) {
3+
this.val = val;
4+
this.left = left;
5+
this.right = right;
6+
}
7+
}
8+
9+
var maxLevelSum = function(root) {
10+
if (!root) return 0;
11+
12+
let queue = [root];
13+
let maxSum = -Infinity;
14+
let level = 0;
15+
let resultLevel = 0;
16+
17+
while (queue.length > 0) {
18+
level++;
19+
let levelSum = 0;
20+
let levelSize = queue.length;
21+
22+
for (let i = 0; i < levelSize; i++) {
23+
let node = queue.shift();
24+
levelSum += node.val;
25+
26+
if (node.left) queue.push(node.left);
27+
if (node.right) queue.push(node.right);
28+
}
29+
30+
if (levelSum > maxSum) {
31+
maxSum = levelSum;
32+
resultLevel = level;
33+
}
34+
}
35+
36+
return resultLevel;
37+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
class Solution:
9+
def maxLevelSum(self, root: TreeNode) -> int:
10+
from collections import deque
11+
12+
if not root:
13+
return 0
14+
15+
queue = deque([root])
16+
max_sum = float('-inf')
17+
max_level = 1
18+
current_level = 1
19+
20+
while queue:
21+
level_sum = 0
22+
level_size = len(queue)
23+
24+
for _ in range(level_size):
25+
node = queue.popleft()
26+
level_sum += node.val
27+
28+
if node.left:
29+
queue.append(node.left)
30+
if node.right:
31+
queue.append(node.right)
32+
33+
if level_sum > max_sum:
34+
max_sum = level_sum
35+
max_level = current_level
36+
37+
current_level += 1
38+
39+
return max_level

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
327327
- 2026-01-03 — [Number of Ways to Paint N × 3 Grid](https://leetcode.com/problems/number-of-ways-to-paint-n-3-grid/) (Hard) → `Hard/2026-01-03-1411-Number-of-Ways-to-Paint-N-3-Grid`
328328
- 2026-01-04 — [Four Divisors](https://leetcode.com/problems/four-divisors/) (Medium) → `Medium/2026-01-04-1390-Four-Divisors`
329329
- 2026-01-05 — [Maximum Matrix Sum](https://leetcode.com/problems/maximum-matrix-sum/) (Medium) → `Medium/2026-01-05-1975-Maximum-Matrix-Sum`
330+
- 2026-01-06 — [Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/) (Medium) → `Medium/2026-01-06-1161-Maximum-Level-Sum-of-a-Binary-Tree`

0 commit comments

Comments
 (0)