/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if (root == nullptr) return vector<vector<int> >();
vector<vector<int> > ret;
vector<int> ans;
reSolver(root, sum, ans, ret);
return ret;
}
void reSolver(TreeNode* root, int sum, vector<int>& ans, vector<vector<int> >& ret) {
ans.emplace_back(root->val);
if (root->left != nullptr) {
reSolver(root->left, sum - root->val, ans, ret);
}
if (root->right != nullptr) {
reSolver(root->right, sum - root->val, ans, ret);
}
if (root->left == nullptr && root->right == nullptr && sum == root->val) {
ret.emplace_back(ans);
}
ans.pop_back();
return;
}
};
113. Path Sum II
最新推荐文章于 2024-09-21 10:02:17 发布