129. Sum Root to Leaf Numbers

class Solution {
    public int sumNumbers(TreeNode root) {
        return sumNumber("", root);
    }
    private int sumNumber(String str, TreeNode node){
        if(node==null)
            return 0;
        str+=Integer.toString(node.val);
        if(node.left==null&&node.right==null)
            return Integer.parseInt(str);
        int res=0;
        if(node.left!=null)
            res+=sumNumber(str, node.left);
        if(node.right!=null)
            res+=sumNumber(str, node.right);
        return res;
    }
}

  

posted @ 2017-10-04 05:44  Weiyu Wang  阅读(120)  评论(0编辑  收藏  举报