path sum II

/**

 * Definition for a binary tree node.

 * struct TreeNode {

 *     int val;

 *     TreeNode *left;

 *     TreeNode *right;

 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}

 * };

 */

class Solution {

public:

    vector<vector<int>> pathSum(TreeNode* root, int sum) {

        vector<vector<int>> vv,vv2;

        

        if(root==NULL) return vv;

        vector<int> v;

          

        if(root->left ==NULL && root->right == NULL) {

            if(root->val==sum){

                v.push_back(root->val);

                vv.push_back(v);

            }

            return vv;

        }

            

        vv = pathSum(root->left,sum-root->val);

        if(!vv.empty()){

            for(int i=0;i<vv.size();i++){

                vv[i].insert(vv[i].begin(),root->val);

            }

        }

       

        vv2 = pathSum(root->right,sum-root->val);

        if(!vv2.empty()){

            for(int i=0;i<vv2.size();i++){

                vv2[i].insert(vv2[i].begin(),root->val);

                vv.push_back(vv2[i]);

            }

        }

        

        return vv;

    }

};

 

你可能感兴趣的:(Path)