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);
        }
    }
}

 

posted @ 2022-05-30 10:49  java架构师1  阅读(57)  评论(0)    收藏  举报