Path Sum II

来源:互联网 发布:list去除重复数据 编辑:程序博客网 时间:2024/05/16 18:57
/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {        void DFS(TreeNode *root,int sum,vector<vector<int>> &ans,vector<int> &vec){                        if(root->left==NULL&&root->right==NULL){            if(sum==root->val){                vec.push_back(root->val);                ans.push_back(vec);                vec.pop_back();            }            return;        }        if(root->left!=NULL){            vec.push_back(root->val);            DFS(root->left,sum-root->val,ans,vec);            vec.pop_back();        }                if(root->right!=NULL){            vec.push_back(root->val);            DFS(root->right,sum-root->val,ans,vec);                vec.pop_back();        }    }public:    vector<vector<int> > pathSum(TreeNode *root, int sum) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<vector<int>> ret;        vector<int> vec;        if(root==NULL)return ret;        bool found=false;        DFS(root,sum,ret,vec);        return ret;            }};

原创粉丝点击