## Algorithm

Problem Name: 1161. Maximum Level Sum of a Binary Tree

Given the `root` of a binary tree, the level of its root is `1`, the level of its children is `2`, and so on.

Return the smallest level `x` such that the sum of all the values of nodes at level `x` is maximal.

Example 1:

```Input: root = [1,7,0,7,-8,null,null]
Output: 2
Explanation:
Level 1 sum = 1.
Level 2 sum = 7 + 0 = 7.
Level 3 sum = 7 + -8 = -1.
So we return the level with the maximum sum which is level 2.
```

Example 2:

```Input: root = [989,null,10250,98693,-89388,null,null,null,-32127]
Output: 2
```

Constraints:

• The number of nodes in the tree is in the range `[1, 104]`.
• `-105 <= Node.val <= 105`

## Code Examples

### #1 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public int maxLevelSum(TreeNode root) {
if (root == null) {
return 0;
}
int currLevel = 1;
int maxSum = Integer.MIN_VALUE;
int maxSumLevel = 0;
Queue < TreeNode> queue = new LinkedList<>();
while (!queue.isEmpty()) {
int size = queue.size();
int currSum = 0;
while (size-- > 0) {
TreeNode removed = queue.remove();
currSum += removed.val;
if (removed.left != null) {
}
if (removed.right != null) {
}
}
if (maxSum  <  currSum) {
maxSum = currSum;
maxSumLevel = currLevel;
}
currLevel++;
}
return maxSumLevel;
}
}
``````
Copy The Code &

Input

cmd
root = [1,7,0,7,-8,null,null]

Output

cmd
2

### #2 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const maxLevelSum = function(root) {
if (root == null) return 0
let res = 1
let cur = [root]
let next = []
let max = Number.MIN_SAFE_INTEGER
let sum = 0
let level = 1
while (cur.length) {
let node = cur.pop()
if (node.left) next.push(node.left)
if (node.right) next.push(node.right)
sum += node.val
if (cur.length === 0) {
cur = next
next = []
if (sum > max) {
res = level
max = sum
}
sum = 0
level++
}
}

return res
}

// DFS

const maxLevelSum = function(root) {
let result = {};
let recursion = function(root, level) {
if (result[level] !== undefined) {
result[level] += root.val;
} else {
result[level] = root.val;
}
if (root.left !== null) {
recursion(root.left, level + 1);
}
if (root.right !== null) {
recursion(root.right, level + 1);
}
};
recursion(root, 1);
let resultkey = 1;
let max = Number.MIN_VALUE;
for (let key of Object.keys(result)) {
if (result[key] > max) {
max = result[key];
resultkey = key;
}
}
return Number(resultkey);
};

``````
Copy The Code &

Input

cmd
root = [1,7,0,7,-8,null,null]

Output

cmd
2

### #3 Code Example with Python Programming

```Code - Python Programming```

``````
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
def maxLevelSum(self, root: TreeNode) -> int:
levels, l, q = [], 1, [root]
while q:
levels.append([sum(node.val for node in q), l])
l += 1
q = [child for node in q for child in (node.left, node.right) if child]
return sorted(levels, key = lambda x: (x[0], -x[1]))[-1][1]``````
Copy The Code &

Input

cmd
root = [989,null,10250,98693,-89388,null,null,null,-32127]

Output

cmd
2

### #4 Code Example with C# Programming

```Code - C# Programming```

``````
using System.Collections.Generic;

namespace LeetCode
{
public class _1161_MaximumLevelSumOfABinaryTree
{
public int MaxLevelSum(TreeNode root)
{
var currentLevel = 1;
int maxSum = int.MinValue, maxLevel = 1;

var queue = new Queue < TreeNode>();
queue.Enqueue(root);
while (queue.Count > 0)
{
var sum = 0;
var size = queue.Count;
for (int i = 0; i  <  size; i++)
{
var node = queue.Dequeue();
sum += node.val;
if (node.left != null) queue.Enqueue(node.left);
if (node.right != null) queue.Enqueue(node.right);
}

if (sum > maxSum)
{
maxLevel = currentLevel;
maxSum = sum;
}

currentLevel++;
}

return maxLevel;
}
}
}
``````
Copy The Code &

Input

cmd
root = [989,null,10250,98693,-89388,null,null,null,-32127]

Output

cmd
2