My c++ solution


  • 0
    Z
    class Solution {
    vector<vector<int>> res;
    int nums[1000];
    public:
    void path(TreeNode* root, int sum, int* nums, int pos)
    {
        if (root == NULL)
    	{
    		return;
    	}
        if(root->left==NULL&&root->right==NULL)
        {
            if(sum==root->val)
            {
                nums[pos]=sum;
                vector<int>v;
                for(int i=0;i<=pos;i++)
                {
                    v.push_back(nums[i]);
                }
                res.push_back(v);
                return;
            }
            return;
        }
        nums[pos]=root->val;
        pos++;
        path(root->left, sum-root->val, nums, pos);
        path(root->right, sum-root->val, nums, pos);
    }
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        if(root == NULL)
            return res;
        path(root, sum, nums, 0);
        return res;
    }};

Log in to reply
 

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