Simple Recursive Solution in C++


  • 0
    F

    Simple Recursive Solution in C++

    ''''
    class Solution {
    public:
    int sumNumbers(TreeNode* root) {
    return sumHelper(root,0);
    }

    int sumHelper(TreeNode* node, int sum) {
        if(node == NULL) {
            return 0;
        }
        if(node->right==NULL && node->left == NULL) {
            return node->val + (sum*10);
        }
        return sumHelper(node->left, node->val +(sum*10)) +sumHelper(node->right, node->val +(sum*10)) ;
        
    }
    

    };
    '''


Log in to reply
 

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