112题——Path Sum(二叉树,深度优先搜索,用了递归和迭代的方法)
2015-04-24 11:10
447 查看
Path Sum
Total Accepted: 50593 Total Submissions: 169742My SubmissionsQuestion 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->2which 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() { }
相关文章推荐
- 二叉树的几种遍历方法,包括递归和迭代
- 二叉树的3中遍历方法(递归和迭代)
- 二叉树 前序中序后序层序遍历 迭代、递归方法
- 根据前序跟中序遍历的结果建立二叉树——迭代和递归的方法
- 逆转链方法遍历二叉树 不使用栈与递归 Robson方法 与 Morris方法
- 43 递归和非递归俩种方法实现二叉树的三种遍历
- 三着色问题 递归和迭代方法 打印着色方案
- 二叉树三种遍历的递归和迭代解法
- 二叉树非递归遍历方法
- 递归方法 前序 中序 后序 遍历二叉树
- 用递归方法对二叉树进行先序、中序和后序遍历
- 二叉树中包括高度,深度,节点数,宽度,最大值等的递归和非递归方法
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)及运行实例结果
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- Morris Traversal方法遍历二叉树(非递归,不用栈,O(1)空间)
- java二叉树的遍历,递归与非递归方法
- 二叉树的三种遍历方式(递归和非递归的实现方法)
- JAVA:递归和迭代两种方法计算斐波那契数列
- 非递归方法前序遍历二叉树
- 二叉树的三种遍历方法(递归)