剑指 Offer 34. 二叉树中和为某一值的路径 - 力扣(LeetCode)
class Solution {
public:
vector<vector<int>> con;
int k;
vector<vector<int>> pathSum(TreeNode* root, int target) {
if (!root) return con;
k = target;
vector<int> emptyCon;
SumAndCheck(root, 0, emptyCon);
return con;
}
void SumAndCheck(TreeNode* T, int _sum, vector<int> path) {
_sum += T->val;
path.push_back(T->val);
if (T->left) SumAndCheck(T->left, _sum, path);
if (T->right) SumAndCheck(T->right, _sum, path);
if (!T->left && !T->right && _sum == k) con.push_back(path);
}
};