[leetcode]404. Sum of Left Leaves左叶子之和

Posted stAr_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode]404. Sum of Left Leaves左叶子之和相关的知识,希望对你有一定的参考价值。

弄个flag记录是不是左节点就行

int res = 0;
    public int sumOfLeftLeaves(TreeNode root) {
        if (root==null) return res;
        leftSum(root,false);
        return res;
    }
    private void leftSum(TreeNode root,boolean flag)
    {
        if (root.left==null&&root.right==null&&flag)
            res+=root.val;
        if (root.left!=null)
            leftSum(root.left,true);
        if (root.right!=null)
            leftSum(root.right,false);
    }

 

以上是关于[leetcode]404. Sum of Left Leaves左叶子之和的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 404. Sum of Left Leaves

leetcode-404. Sum of Left Leaves

[leetcode-404-Sum of Left Leaves]

LeetCode 404. Sum of Left Leaves

LeetCode - 404. Sum of Left Leaves

leetcode 404 Sum of Left Leaves