其他分享
首页 > 其他分享> > leetcode 每日一题

leetcode 每日一题

作者:互联网

leetcode 每日一题 1022. 从根到叶的二进制数之和

class Solution {
    int sum = 0;

    public int sumRootToLeaf(TreeNode root) {
        f(root, 0);
        return sum;
    }

    private void f(TreeNode root, int i) {
        if(root.left == null && root.right == null){
            sum += (i<<1)+root.val;
            return;
        }
        if(root.left != null){
            f(root.left, (i<<1) + root.val);
        }
        if(root.right != null){
            f(root.right, (i<<1) + root.val);
        }
    }
}

 

标签:TreeNode,int,每日,sum,null,root,leetcode
来源: https://www.cnblogs.com/yexuba/p/16326034.html