您的位置:首页 > 其它

112题——Path Sum(二叉树,深度优先搜索,用了递归和迭代的方法)

2015-04-24 11:10 447 查看

Path Sum

Total Accepted: 50593 Total Submissions: 169742My Submissions
Question Solution

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.

Hide Tags
Tree Depth-first Search

Have you met this question in a real interview?
Yes

No

Discuss

这道题需要查找出是否有路径等于给定值,采用深度优先搜索的方法,在每次往下搜索时,记录下到当前结点的路径,然后若到了叶节点时

就判断下是否与之相等,再将判断的结果回溯就可以了:

#include<iostream>
#include<stack>
#include<set>

using namespace std;
// Definition for binary tree
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

bool ifsum(TreeNode* root,int sum,int cursum)
{
if(root==NULL)
return false;
if(root->left==NULL&&root->right==NULL)
return cursum+root->val==sum;

if(root->left!=NULL||root->right!=NULL)
return ifsum(root->left,sum,cursum+root->val)||ifsum(root->right,sum,cursum+root->val);
}
bool hasPathSum(TreeNode *root, int sum) {
return ifsum(root,sum,0);
}
int main()
{

}


  还看到有一种基本与上面类似的方法,只是将当前的路径放在了外面作为迁居变量,而这里需要注意的是,在每次向下搜索完了之后,要记得在之前加了当前结点值的要减掉这个值

#include<iostream>
#include<stack>
#include<set>

using namespace std;
// Definition for binary tree
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

int sum_temp;
int targetsum;
bool ifsum(TreeNode* root)
{
if(root==NULL)
return false;
if(root->left==NULL&&root->right==NULL)
return targetsum+root->val==sum_temp;

targetsum+=root->val;
bool res1=ifsum(root->left);
bool res2=ifsum(root->right);

targetsum-=root->val;//这里由于是全局变量,所以在前面加了之后,在这个结点算完后,要减掉

return res1||res2;
}

bool hasPathSum(TreeNode *root, int sum) {
sum_temp=sum;
targetsum=0;
return ifsum(root);
}
int main()
{

}


深度优先搜索的思想,这里用到了迭代的方法,采用堆栈。对其进行深度优先搜索,在这,对于每一个节点,创造一个pair变量,一个是节点,另外一个是从根节点到次节点的长度,然后深度优先搜索,遇到叶节点返回,其他的结点就一次往下导入

#include<iostream>
#include<stack>
#include<utility>
//#include<set>
//#include<map>
using namespace std;
// Definition for binary tree
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

bool hasPathSum(TreeNode *root, int sum) {
if(root==NULL)
return false;
stack<pair<TreeNode*,int> > sta_temp;
sta_temp.push(make_pair(root,root->val));
TreeNode* node_temp;
int sum_temp;
while(!sta_temp.empty())
{
node_temp=sta_temp.top().first;
sum_temp=sta_temp.top().second;
sta_temp.pop();
if(node_temp->left==NULL&&node_temp->right==NULL)
{
if(sum_temp==sum)
return true;
}
if(node_temp->left!=NULL)
{
sta_temp.push(make_pair(node_temp->left,node_temp->left->val+sum_temp));
}
if(node_temp->right!=NULL)
{
sta_temp.push(make_pair(node_temp->right,node_temp->right->val+sum_temp));
}
}
return false;
}
int main()
{

}


  

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: