[Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
利用前序和中序遍历构造二叉树的思想与利用中序和后续遍历的思想一样,不同的是,全树的根节点为preorder数组的第一个元素,具体分析过程参照利用中序和后续构造二叉树。这两道题是从二叉树的前序遍历、中序遍历、后续遍历这三种遍历中选取两种方式构造二叉树,总的思路都是先确定全树根节点,然后在中序遍历中找到根节点,从而确定全树的左、右子树(题目要求没有重复元素,所以可以确定),最后以两子树为新的二叉树递归调用原函数。值得注意的是:利用三种遍历构造二叉树,要具备两条件,一、知道根节点、二,能区分左右子树,所以不能用前序遍历和后续遍历构造二叉树(不知道是否有大神可以)。代码如下
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
{
int len=preorder.size();
return build(preorder,,len-,inorder,,len-);
} TreeNode *build(vector<int> &preorder,int preBeg,int preEnd,vector<int> &inorder,int inBeg,int inEnd)
{
if(preBeg>preEnd||inBeg>inEnd) return NULL;
TreeNode *root=new TreeNode(preorder[preBeg]); for(int i=;i<inorder.size();++i)
{
if(inorder[i]==preorder[preBeg])
{
root->left=build(preorder,preBeg+,preBeg+i-inBeg,inorder,inBeg,i-);
root->right=build(preorder,preBeg+i+-inBeg,preEnd,inorder,i+,inEnd);
}
}
return root;
}
};
[Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树的更多相关文章
- leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume t ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...
- 105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树
给定一棵树的前序遍历与中序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出前序遍历 = [3,9,20,15,7]中序遍历 = [9,3,15,20,7]返回如下的二叉树: ...
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...
- Leetcode Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- tomcat createSecureRandom 花费了将近10分钟
http://www.th7.cn/Program/java/201603/776312.shtml 启动tomcat很慢,检查后发现:[localhost-startStop-1] org.apac ...
- Oracle锁表处理
最近系统连续出现好几次锁表,昨晚又发生一次锁表,11点钟跑到客户现场,进过跟踪发现导致这次锁表的机器和上一次是同一台,花了近半小时解锁.之后到科室找到那台机器看看情况,发现那台机器速度超慢,保存一份病 ...
- VIM第七版
ZZ:退出并保存 e!:退回到上次保存时的样子 cw:修改单词(自动进入插入模式) cc:修改一整行的内容 cs:修改一个词(自动进入插入模式) .:可以重复上一个命令 J:将下一行内容合并到本行末尾 ...
- 2019年猪年海报PSD模板-第五部分
14套精美猪年海报,免费猪年海报,下载地址:百度网盘,https://pan.baidu.com/s/1CuZKPmFbbSBvzSXoCt2few
- mysql新手进阶03
当年忠贞为国酬,何曾怕断头? 如今天下红遍,江山靠谁守? 业未就,身躯倦,鬓已秋. 你我之辈,忍将夙愿,付与东流? 数据库结构如下: 仓库(仓库号, 城市, 面积) 订购单(职工号, 供应商号, 订购 ...
- lintcode407 加一
加一 给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组. 该数字按照大小进行排列,最大的数在列表的最前面. 您在真实的面试中是否遇到过这个题? Yes 样例 给定 [1,2,3] ...
- Python字符串所有操作函数
name = "my \tname is {name} and i am {year} old" print(name.capitalize())#首字母大写 print(name ...
- 关于Filter中ServletRequest强转HttpServletRequest问题
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE ...
- 京东2018秋招c++岗 神奇数
题意大概是: 一个数比如242,把所有数字分成两组,而且两组的和相等,那么这个数就是神奇数,此时242,能够分成{2,2}和{4},所以242是神奇数. 题目要求输入n和m求[n,m]区间内神奇数的个 ...
- Icingaweb2监控oracle数据库的安装配置流程
Icinga2安装配置check_oracle_health流程 1.安装 由于check_oracle_health是使用perl语言编写的,因此在安装该插件之前,首先要安装oracle的客户端实例 ...