题目:
题解:
class Solution {
public:vector<vector<int>> ret;unordered_map<TreeNode*, TreeNode*> parent;void getPath(TreeNode* node) {vector<int> tmp;while (node != nullptr) {tmp.emplace_back(node->val);node = parent[node];}reverse(tmp.begin(), tmp.end());ret.emplace_back(tmp);}vector<vector<int>> pathSum(TreeNode* root, int targetSum) {if (root == nullptr) {return ret;}queue<TreeNode*> que_node;queue<int> que_sum;que_node.emplace(root);que_sum.emplace(0);while (!que_node.empty()) {TreeNode* node = que_node.front();que_node.pop();int rec = que_sum.front() + node->val;que_sum.pop();if (node->left == nullptr && node->right == nullptr) {if (rec == targetSum) {getPath(node);}} else {if (node->left != nullptr) {parent[node->left] = node;que_node.emplace(node->left);que_sum.emplace(rec);}if (node->right != nullptr) {parent[node->right] = node;que_node.emplace(node->right);que_sum.emplace(rec);}}}return ret;}
};