2023每日刷题(五十七)
Leetcode—113.路径总和II
实现代码
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:vector<vector<int>> res;vector<int> path;void dfs(TreeNode* root, int sum) {if(root == nullptr) {return;}sum -= root->val;path.push_back(root->val);if(sum == 0 && root->left == nullptr && root->right == nullptr) {res.push_back(path);}dfs(root->left, sum);dfs(root->right, sum);path.pop_back();}vector<vector<int>> pathSum(TreeNode* root, int targetSum) {dfs(root, targetSum);return res;}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!