# LeetCode题解——Path Sum II

240人阅读 评论(0)

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
/ \
4   8
/   / \
11  13  4
/  \    / \
7    2  5   1


return

[
[5,4,11,2],
[5,8,4,5]
]


vector<vector<int>> pathSum(TreeNode* root, int sum) {} 这是我们需要实现的功能函数

 <span style="white-space:pre">	</span>vector<vector<int>> pathsleft;
vector<vector<int>> pathsright;
if(root->left) pathsleft = pathSum(root->left,sum-root->val);
if(root->right) pathsright = pathSum(root->right,sum-root->val);
for(auto p:pathsleft){
p.insert(p.begin(),root->val);
paths.push_back(p);
}
for(auto p:pathsright){
p.insert(p.begin(),root->val);
paths.push_back(p);
}

        if(!root->left && !root->right){
if(sum==root->val){
vector<int> t;
t.push_back(sum);
paths.push_back(t);
}
return paths;
}

/**
* 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) {
vector<vector<int>> paths;
if(!root) return paths;
if(!root->left && !root->right){
if(sum==root->val){
vector<int> t;
t.push_back(sum);
paths.push_back(t);
}
return paths;
}
vector<vector<int>> pathsleft;
vector<vector<int>> pathsright;
if(root->left) pathsleft = pathSum(root->left,sum-root->val);
if(root->right) pathsright = pathSum(root->right,sum-root->val);
for(auto p:pathsleft){
p.insert(p.begin(),root->val);
paths.push_back(p);
}
for(auto p:pathsright){
p.insert(p.begin(),root->val);
paths.push_back(p);
}
return paths;
}
};

<span style="font-family:Times New Roman;">  vector<vector<int>> paths;
vector<vector<int>> pathsleft;
vector<vector<int>> pathsright;</span>

void FindPaths(TreeNode* root,int sum,vector<int>& path,vector<vector<int>>& paths)

paths只会在找到一条符合条件的路径path时才会push_back(path);其余时候，paths不会改变。

    void FindPaths(TreeNode* root,int sum,vector<int>& path,vector<vector<int>>& paths){
if(!root) return;
path.push_back(root->val);
if(root->val==sum && !root->left && !root->right){
paths.push_back(path);
}
FindPaths(root->left,sum-root->val,path,paths);
FindPaths(root->right,sum-root->val,path,paths);
path.pop_back();
}

/**
* 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) {
vector<vector<int>> paths;
vector<int> path;
FindPaths(root,sum,path,paths);
return paths;
}
void FindPaths(TreeNode* root,int sum,vector<int>& path,vector<vector<int>>& paths){
if(!root) return;
path.push_back(root->val);
if(root->val==sum && !root->left && !root->right){
paths.push_back(path);
}
FindPaths(root->left,sum-root->val,path,paths);
FindPaths(root->right,sum-root->val,path,paths);
path.pop_back();
}
};

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：249142次
• 积分：4161
• 等级：
• 排名：第7683名
• 原创：159篇
• 转载：159篇
• 译文：0篇
• 评论：44条
阅读排行
最新评论