Monday, November 2, 2015

[leetcode] Sum Root to Leaf Numbers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class Solution {
    public int sumNumbers(TreeNode root) {
        return sumNumbers_rec(root, "");
    }

    private int sumNumbers_rec(TreeNode root, String upper){
        if (root == null) return 0;
        if (root.left == null && root.right == null) return (new Integer(upper+root.val)).intValue();
        return sumNumbers_rec(root.left, upper+root.val)+sumNumbers_rec(root.right, upper+root.val);
    }
}

No comments:

Post a Comment