13 lines C++ DFS


  • 0
    class Solution {
    public:
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
            vector<vector<int>>res;
            if(!root) return res;
            DFS(root, res, vector<int>(), 0, sum);
            return res;
        }
        
        void DFS(TreeNode* root, vector<vector<int>>& res, vector<int>path, int sum, int target){
            if(!root) return;
            path.push_back(root->val);
            sum += root->val;
            if(!root->left && !root->right){
                if(sum == target) res.push_back(path);
                return;
            }
            DFS(root->left, res, path, sum, target);
            DFS(root->right, res, path, sum, target);
        }
    };
    

Log in to reply
 

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