LeetCode-Sum Root to Leaf Numbers

来源:互联网 发布:网络用语sd是什么意思 编辑:程序博客网 时间:2024/04/28 13:12
/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    int sumNumbers(TreeNode *root) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        return sumNum(root, 0);    }        int sumNum(TreeNode *root, int curNum)    {        if (root == NULL)            return 0;        else        {            curNum = curNum * 10 + root->val;            if (root->left == NULL && root->right == NULL)                return curNum;            else                return sumNum(root->left, curNum) + sumNum(root->right, curNum);        }    }};