剑指 Offer 34. 二叉树中和为某一值的路径
class Solution {
public:
vector>res;
vectortempPath;
void dfs(TreeNode*root,int target){
if(!root) return;
tempPath.emplace_back(root->val);
target-=root->val;
if(!root->left&&!root->right&&target==0) //到达叶子结点且符合路径和要求
res.emplace_back(tempPath);
dfs(root->left,target);
dfs(root->right,target);
tempPath.pop_back(); //经过本结点的路径搜索完毕,弹出回溯
}
vector> pathSum(TreeNode* root, int target) {
dfs(root,target);
return res;
}
};



