42 Deepest Leaves Sum

题目

Given a binary tree, return the sum of values of its deepest leaves.
在这里插入图片描述
Constraints:

The number of nodes in the tree is between 1 and 10^4.
The value of nodes is between 1 and 100.
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

分析

题意:求出最高层的叶子之和(按图来说,这个最高层应当是同层唯一的,而非某一支的叶子,比如此处5没有被算进去)。

由于不同的叶子,可能在不同的层次,因此我们得记录叶子所在层次。
一开始想到使用map存储,但是key和value(对应层数和叶子值)都不能保证唯一,因此突然想到好像使用数组就行。

算法:

数组的下标表示当前层数;
遍历的每个值,都累加到当前数组arr[i]中;
返回arr[i](i为max值)即可。

由于是递归,每层递归都要用到i作为层数,因此我们要构造一个辅助函数,把i作为参数传递下去;注意不能定义一个全局i。因为递归实际上有两个分支(左子树和右子树),如果用全局i,会导致左子树i++,右子树也i++,此时层数对应不上。

解答

class Solution {
    int[] tmp = new int[101];
    public int deepestLeavesSum(TreeNode root) {
    	// 初始化的层数i=0
        int[] res = helper(root,0);
        // 从后往前遍历,数组默认值为0,如果非零,就是我们要的最顶层结果
        for(int i=100;i>=0;--i)
            if (res[i]!=0)
                return res[i];
        return 0;
    }
    int[] helper(TreeNode root,int i){
        if(root==null) return null;
        // 将当前值加到数组中,然后层数加1
        tmp[i++]+=root.val;
        helper(root.left,i);
        helper(root.right,i);
        return tmp;
    }
}

我看了下评论区的答案,我的算法竟然是最快的,也是最简洁的。
在这里插入图片描述
这是评论区同复杂度的其他解法;

class Solution {
    public int deepestLeavesSum(TreeNode root) {
        int[] maxDepth = new int[1];
        int[] sumOfDeepestLeaves = new int[1];
        findDepth(root, maxDepth, sumOfDeepestLeaves, 1);
        return sumOfDeepestLeaves[0];
    }
    
    public void findDepth(TreeNode root, int[] maxDepth, int[] sumOfDeepestLeaves, 
                          int currDepth) {
        if(root == null) {
           return; 
        }
        
        if(root.left == null && root.right == null) {
            /** If depth of the current leaf is equal to existing maximum depth, 
                add the value of this leaf to the existing sum of deepest leaves. */
            if(maxDepth[0] == currDepth) {
                sumOfDeepestLeaves[0] += root.val;
                return;
            /** If depth of the current leaf is less than the existing maximum depth,
                dont change the existing sum of deepest leaves and return. */
            } else if(currDepth < maxDepth[0]) {
                return;
            /** If depth of the current leaf is greater than the existing maximum depth,
                set max depth to current depth and also initialize the sum of deepest leaves
                as the current node val */
            } else {
                sumOfDeepestLeaves[0] = root.val;
                maxDepth[0] = currDepth;
                return;
            }
        }
        
        findDepth(root.left, maxDepth, sumOfDeepestLeaves, currDepth+1);
        findDepth(root.right, maxDepth, sumOfDeepestLeaves, currDepth+1);
        return;
    } 
}

好像有点收获?

发布了118 篇原创文章 · 获赞 26 · 访问量 8007

猜你喜欢

转载自blog.csdn.net/weixin_43367550/article/details/104688824