/* 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> > allPath; //存放当前正在遍历的路径 vector<int> tempPath; vector<vector<int> > FindPath(TreeNode* root,int expectNumber) { if(root==NULL) return allPath; tempPath.push_back(root->val); if(root->val==expectNumber && root->left==NULL && root->right==NULL){ allPath.push_back(tempPath); } if(root->val<expectNumber && root->left!=NULL) FindPath(root->left, expectNumber - root->val); if(root->val<expectNumber && root->right!=NULL) FindPath(root->right, expectNumber - root->val); //返回上一个节点 tempPath.pop_back(); return allPath; } }; //看了很多代码,思路几乎就上面一个
原文:https://www.cnblogs.com/loyolh/p/12343066.html