c++ solution


  • 0
    B
    class Solution {
    public:
        vector<vector<int>> result;
        
        bool isleaf(TreeNode *root){
            return (!root->left && !root->right);
        }
        
        void dfs(TreeNode *root, vector<int> cur, int sum){
            if(!root) return ;
            
            cur.push_back(root->val);
            sum-=root->val;
            
            if(sum==0 && isleaf(root)) result.push_back(cur);
            
            dfs(root->left, cur, sum);
            dfs(root->right, cur, sum);
            
            return ;
        }
        
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
            if(!root) return result;
            
            dfs(root, vector<int> (), sum);
            
            return result;
        }
    };
    

Log in to reply
 

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