剑指offer 二叉树中和为某一值的路径

来源:互联网 发布:大数据 云计算 关系 编辑:程序博客网 时间:2024/06/05 16:23
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;}};

0 0