class Solution {
public:
vector<vector<int> > FindPath(TreeNode* root, int expectNumber) {
vector<vector<int> > ans;
vector<int> test;
dfs(ans, test, root, expectNumber);
return ans;
}
void dfs(vector<vector<int> > &ans, vector<int> &test, TreeNode* root, int cur) {
if (!root)
return;
cur -= root->val;
test.push_back(root->val);
if (!cur&&!(root->left)&&!(root->right))
ans.push_back(test);
dfs(ans, test, root->left, cur);
dfs(ans, test, root->right, cur);
test.pop_back();
cur += root->val;
}
};
剑指offer 二叉树中和为某一值的路径
最新推荐文章于 2022-05-20 08:00:00 发布