class Solution {
public:
vector<vector<int>> res;
vector<int> path;
void dfs(TreeNode* root, vector<int> path, int targetSum) {
if (root == nullptr) return;
path.push_back(root->val);
if (root->left == nullptr && root->right == nullptr) {
if (root->val == targetSum) {
res.push_back(path);
return;
}
}
dfs(root->left, path, targetSum - root->val);
dfs(root->right, path, targetSum - root->val);
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
dfs(root, path, targetSum);
return res;
}
};
剑指offer 34:二叉树中和为某一值的路径
最新推荐文章于 2023-06-21 19:29:26 发布