1161. Maximum Level Sum of a Binary Tree

#### QUESTION:

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: [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.

Note:

The number of nodes in the given tree is between 1 and 10^4. -10^5 <= node.val <= 10^5

#### EXPLANATION:

1. 定义一个level，和result，level用来确定层数，而result用来计算结果
2. 定义个list来进行遍历，将root节点放在其中
3. 当list不为空的时候，说明还没有遍历结束，进入循环
4. 定义一个tmplist来保存下一层的结果，同时将level++
5. 循环list计算出结果，同时如果有子节点，就需要将其添加到tmplist中，来进行下一层的计算
6. 如果当前结果比result大，那么就需要将当前level赋值给result
7. 将tmplist赋值给list，这样就可以继续循环4了，一直到树遍历结束

#### SOLUTION:

``````class Solution {
public int maxLevelSum(TreeNode root) {
if(root==null) return 0;
int level = 0,result = 0;
int sum = Integer.MIN_VALUE;
ArrayList<TreeNode> list = new ArrayList<>();
while (!list.isEmpty()){
level++;
ArrayList<TreeNode> tmp = new ArrayList<>();
int tmpSum = 0;
for(TreeNode node: list){
tmpSum+=node.val;