/* 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> > FindPath(TreeNode* root,int expectNumber) { vector<vector<int> >res; vector<int> cur; path(root,expectNumber,res,cur); return res; } void path(TreeNode *root,int sum, vector<vector<int> >&res,vector<int> &cur) { if(root==NULL) return ; cur.push_back(root->val); if(root->left==NULL&&root->right==NULL) { if(sum==root->val) res.push_back(cur); } if(root->left) path(root->left,sum-root->val,res,cur); if(root->right) path(root->right,sum-root->val,res,cur); cur.pop_back(); } };
时间: 2024-11-04 17:56:06