LeetCode404_404. 左叶子之和

LeetCode404_404. 左叶子之和

一、描述

给定二叉树的根节点 root ,返回所有左叶子之和。

示例 1:

在这里插入图片描述

输入: root = [3,9,20,null,null,15,7] 
输出: 24 
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24

示例 2:

输入: root = [1]
输出: 0

提示:

节点数在 [1, 1000] 范围内
-1000 <= Node.val <= 1000

二、题解

方法一:递归

    //AC Your runtime beats 99.90 % of java submissions.
    //102 / 102 test cases passed.	Status: Accepted	Runtime: 8 ms
    public int sumOfLeftLeaves(TreeNode root) {
    
    
        if (root == null) return 0;
        if (root.left != null && root.left.left == null && root.left.right == null)
            return root.left.val + sumOfLeftLeaves(root.right);
        return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
    }

LeetCode 367. 有效的完全平方数
LeetCode 371. 两整数之和
LeetCode 383. 赎金信
LeetCode 387. 字符串中的第一个唯一字符
LeetCode 389. 找不同
LeetCode 404. 左叶子之和
LeetCode 412. Fizz Buzz
LeetCode 414. 第三大的数
LeetCode 415. 字符串相加
LeetCode 434. 字符串中的单词数



声明:
        题目版权为原作者所有。文章中代码及相关语句为自己根据相应理解编写,文章中出现的相关图片为自己实践中的截图和相关技术对应的图片,若有相关异议,请联系删除。感谢。转载请注明出处,感谢。


By luoyepiaoxue2014

B站: https://space.bilibili.com/1523287361 点击打开链接
微博: http://weibo.com/luoyepiaoxue2014 点击打开链接

猜你喜欢

转载自blog.csdn.net/luoyepiaoxue2014/article/details/129992808