Small 4ms C++ Recursive Pre-order Solution


  • 0
    S
    class Solution {
    private:
        int totSum;
    public:
    void Helper(string num,TreeNode* root) {
        if(!root) return;
        if(root->left)  Helper(num+to_string(root->val),root->left);
        if(root->right)  Helper(num+to_string(root->val),root->right);
        if(!root->left && !root->right) totSum+=stoi(num+to_string(root->val));
    }
    int sumNumbers(TreeNode* root) {
        Helper("",root);
        return totSum;
        
    }
    

    };


Log in to reply
 

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