My C++ Solution with 2 parameters


  • 3
    S
    class Solution {
    public:
        int sum(TreeNode*root, int subsum) {
            if(!root)   return 0;
            if(!root->left && !root->right) return subsum*10+root->val;
            return sum(root->left, subsum*10+root->val)+sum(root->right, subsum*10+root->val);
        }
        int sumNumbers(TreeNode* root) {
            return sum(root, 0);
        }
    };

Log in to reply
 

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