Note:
上一道题是看有没有,这道题是把所有符合条件的路径记录下来
我的奇怪的代码:
/**
* 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>> ans;
vector<int> path;
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if(!root) return ans;
dfs(root, sum);
return ans;
}
void dfs(TreeNode* root, int sum){
if(!root) return;
if(!root -> left && !root -> right && sum == root -> val){
path.push_back(root -> val);
ans.push_back(path);
path.pop_back();
return;
}
sum -= root -> val;
path.push_back(root -> val);
dfs(root -> left, sum);
dfs(root -> right, sum);
path.pop_back();
}
};
带佬的代码:
/**
* 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>> ans;
vector<int> path;
vector<vector<int>> pathSum(TreeNode* root, int sum) {
if(root) dfs(root, sum);
return ans;
}
void dfs(TreeNode* root, int sum){
path.push_back(root -> val);
sum -= root -> val;
if(!root -> left && !root -> right){
if(!sum) ans.push_back(path);
} else{
if(root -> left) dfs(root -> left, sum);
if(root -> right) dfs(root -> right, sum);
}
path.pop_back();
}
};