easy understand C++ Solution


  • 0
    class Solution {
    public:
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
            vector<int> res_tmp(0);
            vector<vector<int>> res;
            findSumPath(root,sum,res_tmp,res);
            return res;
        }    
        void findSumPath(TreeNode* t, int sum, vector<int>& res_tmp,vector<vector<int>>& res) {
            if(t==NULL) return;
            res_tmp.push_back(t->val);
            if(t->val == sum && t->left==NULL && t->right==NULL)
                res.push_back(res_tmp);
            if(t->left != NULL) findSumPath(t->left,sum-t->val,res_tmp,res);
            if(t->right != NULL) findSumPath(t->right,sum-t->val,res_tmp,res);
            res_tmp.pop_back();
        }   
    };
    
    

Log in to reply
 

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