LeetCode 求根到叶子节点数字之和

class Solution {
    int result;
    public int sumNumbers(TreeNode root) {
        if(root == null)return 0;
        sumNumber(root, 0);
        return result;
        
    }
    public void sumNumber(TreeNode root , int sum)
    {
        int temp = sum*10 + root.val;
        if(root == null)return;
        if(root.left == null && root.right == null) //达到叶节点
        {
            result += temp;
            return;
        }
        if(root.left != null)
        {
            sumNumber(root.left , temp);
            
        }
        if(root.right != null)
        {
            sumNumber(root.right , temp);
        }
    }
}

 

posted @ 2020-05-09 20:51  贼心~不死  阅读(120)  评论(0编辑  收藏  举报