Simple C++ Solution...Beats 88.93%


  • 0

    class Solution {
    public:

    vector<vector<int>> res;
    vector<int> out;
    
    void getAllPaths(TreeNode* root, int sum, int sum1){
        
        if(root == NULL)
            return;
        
        else{
            
            sum1 += root->val;
            out.push_back(root->val);
            
            if(root->left)
                getAllPaths(root->left, sum, sum1);
            
            if(root->right)
                getAllPaths(root->right, sum, sum1);        
            
            if(sum == sum1 &&  root->left == NULL && root->right == NULL){
                
                if(sum == sum1)
                    res.push_back(out);
            }
            
            out.pop_back();
            return; 
        }    
    }
    
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        
        getAllPaths(root, sum, 0);
        return res;
    }
    

    };


Log in to reply
 

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