题目:
代码(首刷看解析 2024年1月30日):
class Solution {
public:vector<int> temp;vector<vector<int>> res;void recursion(TreeNode* cur, int sum) {if (!cur->left && !cur->right && sum == 0){res.push_back(temp);return;}if (!cur->left && !cur->right) return;if (cur->left) {temp.emplace_back(cur->left->val);recursion(cur->left, sum - cur->left->val);temp.pop_back();}if (cur->right) {temp.emplace_back(cur->right->val);recursion(cur->right, sum - cur->right->val);temp.pop_back();}return;}vector<vector<int>> pathSum(TreeNode* root, int targetSum) {res.clear();temp.clear();if (root == nullptr) return res;temp.emplace_back(root->val);recursion(root, targetSum - root->val);return res;}
};