Simple cpp solution


  • 0
    V
    This post is deleted!

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

Log in to reply
 

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