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 duplicates do not exist in the tree.
说明:
1)二叉树可空
2)思路:a、根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root), 然后在中序序列(InSequence)中查找此根(root),
b、根据中序遍历特点, 知在查找到的根(root) 前边的序列为根的左子树的中序遍历序列, 后边的序列为根的右子树的中序遍历序列。
c、设在中序遍历序列(InSequence)根前边有left个元素. 则在前序序列(PreSequence)中, 紧跟着根(root)的left个元素序列(即PreSequence[1...left]) 为根的 左子树的前序遍历序列, 在后边的为根的右子树的前序遍历序列.而构造左子树问题其实跟构造整个二叉树问题一样,只是此时前序序列为PreSequence[1...left]), 中序序列 为InSequence[0...left-1], 分别为原序列的子串, 构造右子树同样, 显然可以用递归方法解决。
实现:
实现一:
/**
* 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) {
TreeNode *root=NULL;
creatTree(&root,preorder.begin(),preorder.end(),inorder.begin(),inorder.end());
return root;
}
private:
//双指针(TreeNode **t)实现构建二叉树
void creatTree(TreeNode **t,vector<int>::iterator pre_beg,vector<int>::iterator pre_end,vector<int>::iterator in_beg,vector<int>::iterator in_end)
{
if(pre_beg==pre_end) //空树
{
(*t)=NULL;
return;
}
(*t)=new TreeNode(*pre_beg);
vector<int>::iterator inRootPos=find(in_beg,in_end,(*t)->val);//中序遍历中找到根节点,返回迭代指针
int leftlen=distance(in_beg,inRootPos);//中序遍历起点指针与找到的根节点指针的距离
creatTree(&((*t)->left),next(pre_beg),next(pre_beg,leftlen+),in_beg,inRootPos);//递归构建左子数
creatTree(&((*t)->right),next(pre_beg,leftlen+),pre_end,next(inRootPos),in_end);//递归构建右子树
}
};
实现二:
/**
* 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) {
TreeNode *root=NULL;
creatTree(root,preorder.begin(),preorder.end(),inorder.begin(),inorder.end());
return root;
}
private:
//指针引用(TreeNode *&t)实现构建二叉树
void creatTree(TreeNode *&t,vector<int>::iterator pre_beg,vector<int>::iterator pre_end,vector<int>::iterator in_beg,vector<int>::iterator in_end)
{
if(pre_beg==pre_end) //空树
{
t=NULL;
return;
}
t=new TreeNode(*pre_beg);
vector<int>::iterator result=find(in_beg,in_end,t->val);//中序遍历中找到根节点,返回迭代指针
int len=distance(in_beg,result);//中序遍历起点指针与找到的根节点指针的距离
creatTree(t->left,pre_beg+,pre_beg+len+,in_beg,result);//递归构建左子数
creatTree(t->right,pre_beg+len+,pre_end,result+,in_end);//递归构建右子树
}
};
leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)的更多相关文章
- 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 | 根据前序和中序遍历构建二叉树 | Medium
要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...
- [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 tha ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树
给定一棵树的前序遍历与中序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出前序遍历 = [3,9,20,15,7]中序遍历 = [9,3,15,20,7]返回如下的二叉树: ...
- [LeetCode] 105. 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 105. 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 105. 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 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- easyui中datagrid空数据集不刷新的解决方式
datagrid空间可以异步请求json数据,并将新数据覆盖原有数据,重绘数据表. 但是当回来空数据集的时候,js会产生这样一条错误: TypeError: rows is null for(var ...
- ubuntu添加新用户并添加管理员权限
Ubuntu创建新用户并增加管理员权限 Family 2014-06-24 22:21:22 $是普通管员,#是系统管理员,在Ubuntu下,root用户默认是没有密码的,因此也就无法使用(据说是为 ...
- topcoder(BinaryCode)
Problem Statement Let's say you have a binary string such as the following: 011100011 One way t ...
- eclipse快捷键及各种设置
1.提示键配置一般默认情况下,Eclipse ,MyEclipse 的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多选项 ...
- POJ3585:Accumulation Degree(换根树形dp)
Accumulation Degree Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3425 Accepted: 85 ...
- webapp的favicon应该怎样组织代码
处理过那么多index 页面了那么关于特别针对于此页的favicon是时候详细的总结一下了 它是网站的头像,它出现在浏览器的收藏夹中(标题的旁边) ,浏览器标签页的左上角,微信公众号的logo,保存网 ...
- HDU 1153 magic bitstrings(读题+)
hdu 1153 magic bitstrings 题目大意 一个质数p,现在让你求一个p-1长度的“01魔法串”.关于这个魔法串是这么定义的: 我们现在把这个串经过一段处理变成一个长宽均为p ...
- Java并发编程--CyclicBarrier
概述 CyclicBarrier是一个同步工具类,它允许一组线程互相等待,直到到达某个公共屏障点.与CountDownLatch不同的是该barrier在释放等待线程后可以重用,所以称它为循环(Cyc ...
- 给dedeCMS自定义模型添加图片集字段
1.先找到dedecms图片集模型的templets生成图片集的html代码(album_add.htm) <tr> <td height="24" ...
- post传参部分数据丢失
tomcat获取post传的参数,只接收到前半部分参数,后半部分参数没有接收到 可能的原因是: tomcat中maxParameterCount是用来限制请求中的最大参数量,默认是10000,如果超过 ...