1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| void FindPathHelper(vector<vector<int> > &res, vector<int> one_res, int sum, TreeNode* root, int n) { if (root) { sum += root->val; one_res.push_back(root->val); if (sum == n && !root->left && !root->right) { res.push_back(one_res); one_res.clear(); sum = 0; } FindPathHelper(res, one_res, sum, root->left, n); FindPathHelper(res, one_res, sum, root->right, n); } } bool compare(vector<int> a, vector<int> b) return a.size() > b.size(); vector<vector<int> > FindPath(TreeNode* root,int expectNumber) { vector<vector<int> > res; FindPathHelper(res, {}, 0, root, expectNumber); sort(res.begin(), res.end(), compare); return res; }
|