C++ 17 ms recursion


  • 3
    L
    class Solution {
    public:
        vector<vector<int> > pathSum(TreeNode *root, int sum) {
            vector<vector<int> > result;
            vector<int> path_num;
            helper(root, sum, path_num, result);
            return result;
        }
        void helper(TreeNode *root, int sum, vector<int> &path_num, vector<vector<int> > &result) {
            if(root == NULL)
                return;
            path_num.push_back(root->val);
            if(root->left == NULL && root->right == NULL && sum == root->val) {
                result.push_back(path_num);
                path_num.pop_back();
                return;
            }
            helper(root->left, sum - root->val, path_num, result);
            helper(root->right, sum - root->val, path_num, result);
            path_num.pop_back();
        }
    };

Log in to reply
 

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