您的位置:首页 > 其它

[leetCode]Path Sum&&Path Sum II

2014-03-09 17:18 351 查看

Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and
sum = 22
,

5
/ \
4   8
/   / \
11  13  4
/  \      \
7    2      1

return true, as there exist a root-to-leaf path
5->4->11->2
which sum is 22.

Path Sum很好做,只要递归时sum为0则返回true就好了。

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if(root == NULL) return false;
sum = sum - root->val;
bool isThisPath = false;
if(root->left != NULL)    isThisPath = hasPathSum(root->left,sum);
if(isThisPath)    return true;
if(root->right != NULL) isThisPath = hasPathSum(root->right,sum);
if(isThisPath)    return true;
if(root->left == NULL && root->right == NULL && sum == 0) return true;
if(root->left == NULL && root->right == NULL && sum != 0) return false;
return isThisPath;
}
}


Path Sum II

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]
]

Path SumII 做的稍微久一点,一开始递归没想清楚。

Path SumII是找从根节点到叶结点的节点中有多少条分支的和可以为一个确定值的问题,输出是路径的集合。

用递归来思考的话还是很简单的,每个节点只要确定他的孩子节点是不是在路径上(即孩子节点是不是在和为定值sum的路径上),如果是,则把自己加入,如果不是,则不加入。

递归的结束条件则是 如果节点为叶子节点,且sum在此时为0.

这个基础上,两种解法:

解法一是自顶向下,如果父节点发现本身是在和为定值的路径上(使用Path SumI的结果),则将自己加入路径节点中,然后遍历自己的子节点,依次循环,最后在叶结点中将整条路径加入路径的集合中。

解法一代码:

/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > retvv;
bool hasPathSum(TreeNode *root, int sum) {
if(root == NULL) return false;
sum = sum - root->val;
bool isThisPath = false;
if(root->left != NULL)    isThisPath = hasPathSum(root->left,sum);
if(isThisPath)    return true;
if(root->right != NULL) isThisPath = hasPathSum(root->right,sum);
if(isThisPath)    return true;
if(root->left == NULL && root->right == NULL && sum == 0) return true;
if(root->left == NULL && root->right == NULL && sum != 0) return false;
return isThisPath;
}
vector<vector<int> > pathSum(TreeNode *root, int sum){
vector<int> path;
if(root== NULL)    return retvv;
return onePath(root,sum,path);
}
vector<vector<int> > onePath(TreeNode *root, int sum, vector<int> path){
if(root == NULL)    path.clear();
int cursum = sum - root->val;
if(root->left == NULL && root->right == NULL && cursum == 0){
path.push_back(root->val);
retvv.push_back(path);
return retvv;
}
if(root->left == NULL && root->right == NULL && cursum != 0){
return retvv;
}
if(hasPathSum(root,sum)) {
path.push_back(root->val);
if(hasPathSum(root->left,cursum))
onePath(root->left,cursum,path);
if(hasPathSum(root->right,cursum))
onePath(root->right,cursum,path);
}
return retvv;
}
};


解法二是自下向上,每次先确定其孩子节点在不在路径中,如果孩子节点在,则把自己加入路径中。

解法二代码:

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<vector<int> > retvv;
vector<vector<int> > pathSum(TreeNode *root, int sum) {
if(root == NULL)    return retvv;
sum = sum - root->val;
if(root->left == NULL && root->right == NULL && sum == 0){
vector<int> vec;
vec.push_back(root->val);
retvv.push_back(vec);
return retvv;
}
if(root->left == NULL && root->right == NULL && sum != 0){
return retvv;
}
size_t preSize = retvv.size();
if(root->left != NULL) pathSum(root->left,sum);
if(root->right != NULL) pathSum(root->right,sum);
for(preSize; preSize < retvv.size();preSize++){
retvv[preSize].insert(retvv[preSize].begin(),root->val);
}
return retvv;
}
};


实测解法一代码虽然长一些,但是运行速度比解法二快两倍多(320/840)。猜测应该是在vector中使用insert函数导致的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: