C++简单的递归+回溯
二叉树中和为某一值的路径(二)
http://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<vector<int>> ans;
vector<int> v;
void dfs(TreeNode *root, int sum) {
if(!root) return;
v.push_back(root->val);
if(!root->left && !root->right && sum == root->val)
ans.push_back(v);
dfs(root->left, sum - root->val);
dfs(root->right, sum - root->val);
v.pop_back();
}
vector<vector<int>> FindPath(TreeNode* root,int expectNumber) {
dfs(root, expectNumber);
return ans;
}
};