/**
* 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;
}
};
Path Sum II
最新推荐文章于 2019-04-16 20:08:36 发布