[cpp]
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
//record all the path, though dfs is not time efficient bu also work at most time
public:
void DFS(TreeNode* root, int curSum, vector
{
if(!root)
return;
curSum += root->val;
curPath.push_back(root->val);
if(!root->left && !root->right)
{
if(curSum == sum)
ansPath.push_back(curPath);
return;
}
DFS(root->left, curSum, curPath, ansPath, sum);
DFS(root->right, curSum, curPath, ansPath, sum);
}
vector
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector
vector
DFS(root, 0, curPath, ansPath, sum);
return ansPath;
}
};
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
//record all the path, though dfs is not time efficient bu also work at most time
public:
void DFS(TreeNode* root, int curSum, vector
{
if(!root)
return;
curSum += root->val;
curPath.push_back(root->val);
if(!root->left && !root->right)
{
if(curSum == sum)
ansPath.push_back(curPath);
return;
}
DFS(root->left, curSum, curPath, ansPath, sum);
DFS(root->right, curSum, curPath, ansPath, sum);
}
vector
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector
vector
DFS(root, 0, curPath, ansPath, sum);
return ansPath;
}
};