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 ...
随机推荐
- 异或值 xor
题目描述 给出一个 N 个点的带权无向图,要求从 1 号点到 N 号点的一条路径,使得路径上的边 权异或值最大. 输入格式 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M ...
- Agile已死, Agility长存
注:本文系作者独立翻译,可以随意转载.如有雷同,纯属巧合.原文地址:http://pragdave.me/blog/2014/03/04/time-to-kill-agile/ P.s. 第一次自己翻 ...
- SHH框架的搭建
建立一个Web项目,然后导入如下包 l struts2包:在struts2-blank.war下的lib目录下,以及struts-2.3.15.1\lib下的struts和spring整合的插件包s ...
- matlab默认字体设置
Monospaced Plain 10 SansSerif Plain 10 这是默认设置.希望能帮到你!
- cocos2d学习网址
http://python.cocos2d.org/doc/programming_guide/index.html http://bbs.tairan.com/article-25-1.html h ...
- ldd命令【转】
转自:http://www.cnblogs.com/wanghetao/p/3779611.html ldd命令用于判断某个可执行的 binary 档案含有什么动态函式库. 参数说明: --versi ...
- android hook 框架 ADBI 简介、编译、运行
Android so注入-libinject2 简介.编译.运行 Android so注入-libinject2 如何实现so注入 Android so注入-Libinject 如何实现so注入 A ...
- 解决Django的admin界面中文乱码
解决Django的admin界面中文乱码 问题陈述 最近在做一个很小的Django项目时,使用了自带的sqlite作为数据库.后台admin界面在显示中文数据时,总会遇到乱码.这里截取一小部分代码: ...
- shell脚本中各个参数的意思
文件表达式-e filename 如果 filename存在,则为真-d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真-L ...
- [BZOJ1069][SCOI2007]最大土地面积 凸包+旋转卡壳
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3669 Solved: 1451[Submit][Sta ...