12ms C++ another solution


  • 0
    class Solution {
    public:
        vector<vector<int>> pathSum(TreeNode* root, int sum) {
             vector<vector<int>> result;
            vector<int> vec;
            path(root,sum,result,vec);
            return result;
        }
        void path(TreeNode* root, int sum, vector<vector<int>>& result, vector<int> vec){
        if(root == NULL)
            return;
            int var = root->val;
            vec.push_back(root->val);
            if(root->left == NULL && root->right == NULL)
                if(sum == var)
                    result.push_back(vec);
            if(root->left)
                path(root->left,sum-var,result,vec);
            if(root->right)
                path(root->right,sum-var,result,vec);
        }
    };
    

Log in to reply
 

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