Loading

sum-root-to-leaf-numbers (前序遍历)

class Solution {
public:
    int sumNumbers(TreeNode *root) {
        int sum = 0;
        if (root == NULL) return sum;
        return pre(root, sum);
    }
     int pre(TreeNode *root, int sum)
    {
        if (root == NULL)return 0;
        sum = sum * 10 + root->val;
        if (root->left == NULL&&root->right == NULL){
            return sum;
        }
        return pre(root->left, sum) + pre(root->right, sum);
    }
};

 

posted @ 2018-11-26 10:43  青山新雨  阅读(222)  评论(0编辑  收藏  举报