题目如下:
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]
]
分析如下:
和上一题比较相似,这道题目也可以使用递归来进行搜寻。不同的是,这道题目要求保留所有的正确路径,如果是使用层序遍历,需要存储的东西太多,如果使用递归并且在找到当前的一个解之后回溯,就会比较容易了。我还不太熟悉回溯,于是参考了网上的一些答案,写了回溯的代码。
我的代码:
class Solution {
public:
vector<vector<int> > large_vec;
vector<int> small_vec;
void pathSum_(TreeNode *root, int sum) {
if(root!=NULL&&root->left==NULL&&root->right==NULL&&sum==root->val){
small_vec.push_back(root->val);
large_vec.push_back(small_vec);
return;
}
small_vec.push_back(root->val);
if(root!=NULL&&root->left!=NULL){
pathSum_(root->left, sum-root->val);
small_vec.pop_back();
}
if(root!=NULL&&root->right!=NULL){
pathSum_(root->right,sum-root->val);
small_vec.pop_back();
}
}
vector<vector<int> > pathSum(TreeNode *root, int sum) {
large_vec.clear();
small_vec.clear();
if(root==NULL)
return large_vec;
pathSum_(root, sum);
return large_vec;
}
};
update: 2014- 11- 26
首先,对DFS的回溯理解还是不深刻,重写了一下,和上面的做法基本一样。用这个例子来想回溯,就非常好想了。 sum = 3, 可能的路径有4条。
1
1 1
1 1 1 1
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
void myPathSum(TreeNode *root, int sum, vector<int>& path_result, vector<vector<int> >& all_result) {
if (root->left == NULL && root->right == NULL && sum == root->val) {
path_result.push_back(root->val);
all_result.push_back(path_result);
return;
}else {
path_result.push_back(root->val);
if (root->left != NULL) {
myPathSum(root->left, sum - root->val, path_result, all_result);
path_result.pop_back(); //注意这里要回溯
}
if (root->right != NULL) {
myPathSum(root->right, sum - root->val, path_result, all_result);
path_result.pop_back(); //注意这里要回溯
}
}
}
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<int> path_result;
vector<vector<int> > all_result;
if (root != NULL)
myPathSum(root, sum, path_result, all_result);
return all_result;
}
};
其次,为了代码好些,其实可以牺牲一下空间。上面的代码中,因为表达任意一个候选的单条路径的变量vector<int>& path_result是引用型的,所以必须要回溯,要不然错误。但是如果使用普通的变量来表达vector<int> path_sum,这道题目就很好写代码了。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//使用非引用型的变量,可以不用回溯。
class Solution {
private:
void myPathSum(TreeNode *root, int sum, vector<int> path_result, vector<vector<int> >& all_result) {
if (root->left == NULL && root->right == NULL && sum == root->val) {
path_result.push_back(root->val);
all_result.push_back(path_result);
return;
}else {
path_result.push_back(root->val);
if (root->left != NULL) {
myPathSum(root->left, sum - root->val, path_result, all_result);
}
if (root->right != NULL) {
myPathSum(root->right, sum - root->val, path_result, all_result);
}
}
}
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
vector<int> path_result;
vector<vector<int> > all_result;
if (root != NULL)
myPathSum(root, sum, path_result, all_result);
return all_result;
}
};
有回溯的代码,节省了大量的创建vector<int> path_result的时间, 56ms
没回溯的代码,112ms.
参考资料:
(1) http://yucoding.blogspot.com/2013/04/leetcode-question-67-path-sum-ii.html
(2) 大牛左耳朵耗子的source code