Simple c++ solution


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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.