/**
* 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>> vec;
vector<int> vc;
void check(TreeNode* root,int targetSum,int num){
if(!root)return;
num+=root->val;
vc.push_back(root->val);
if(num==targetSum){
if(root->left==root->right){
vec.emplace_back(vc);
vc.pop_back();
return;
}
}
check(root->left,targetSum,num);
check(root->right,targetSum,num);
vc.pop_back();
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
int num=0;
check(root,targetSum,num);
return vec;
}
};
标签:力扣,right,TreeNode,int,dfs,II,num,root,left
From: https://blog.csdn.net/2303_76711142/article/details/137080232