题目描述
dfs求解路径
样例
/**
* 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>> res_paths;
vector<int> path;
void dfs(TreeNode* root, int sum)
{
if(!root)
return;
sum -= root->val;
path.push_back(root->val);
if(!root->left && !root->right && sum == 0){
res_paths.push_back(path);
}
if(sum > 0)
{
dfs(root->left, sum);
dfs(root->right, sum);
}
path.pop_back();
}
vector<vector<int>> findPath(TreeNode* root, int sum) {
dfs(root, sum);
return res_paths;
}
};