1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
class Solution { public: vector<vector<int>> pathSum(TreeNode* root, int target) { vector<int> tmp; helper(root, target, 0, tmp); return ret; } private: vector<vector<int>> ret; void helper(TreeNode* root, const int& target, int cur, vector<int>& tmp) { if(!root) return; cur += root->val; tmp.push_back(root->val); if(cur == target && !root->left && !root->right) { ret.push_back(tmp); } helper(root->right, target, cur, tmp); helper(root->left, target, cur, tmp); tmp.pop_back(); } };
|