题解 | #二叉树中和为某一值的路径#
二叉树中和为某一值的路径
http://www.nowcoder.com/practice/b736e784e3e34731af99065031301bca
递归
class Solution { public: vector<vector<int>> ans; vector<int> path; void backtra(TreeNode* root,int expectNumber){ if(!root->left&&!root->right){ if(root->val==expectNumber){ path.push_back(root->val); ans.push_back(path); path.pop_back(); }else return; } path.push_back(root->val); if(root->left) backtra(root->left, expectNumber-root->val); if(root->right) backtra(root->right,expectNumber-root->val); path.pop_back(); } vector<vector<int> > FindPath(TreeNode* root,int expectNumber) { if(!root) return ans; backtra(root,expectNumber); return ans; } };
看评论有的说输出结果要排序,题目中好像说的没有啊,也许是题目又变了?