其中 最小 的那个。
示例:
输入:[1,7,0,7,-8,null,null]
输出:2
解释:
第 1 层各元素之和为 1,
第 2 层各元素之和为 7 + 0 = 7,
第 3 层各元素之和为 7 + -8 = -1,
所以我们返回第 2 层的层号,它的层内元素之和最大。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-level-sum-of-a-binary-tree
本题较为容易,只要注意每次递归时层数加1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int[] count;
public Solution(){
count = new int[100000];
}
public int maxLevelSum(TreeNode root) {
Solution solution = new Solution();
solution.func1(1,root);
return solution.func2();
}
public void func1(int n,TreeNode root){
if(root!=null){
count[n]+=root.val;
func1(n+1,root.left);
func1(n+1,root.right);
}
}
public int func2(){
int max=1;
for(int i =1 ; i<100000;i++){
if(count[max]<count[i]) max = i;
}
return max;
}
}