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 ...
随机推荐
- CORS跨域cookie传递
服务端 Access-Control-Allow-Credentials:true Access-Control-Allow-Methods:* Access-Control-Allow-Origin ...
- vue的main.js
import Vue from 'vue'; import App from './App.vue'; //================http 请求======================= ...
- 转:Java NIO(3)
要想讲清楚nio的原理和它的优点得先清楚Java应用程序的文件读写原理和虚拟内存的原理.Java文件读取原理可参见如下图: 当应用程序需要读取文件的时候,内核首先通过DMA技术将文件内容从磁盘读入内核 ...
- Android APK瘦身方法小结
众所周知,APP包体的大小,会影响推广的难度,用户不太喜欢下载太大的APP,同类型同等功能的APP中往往是包体小的更受用户的青睐,所以降低包体是一项非常必要的事情,也是最近公司的APP需要降低包体,所 ...
- mysql 游标的嵌套使用示例
BEGIN DECLARE trainee_no_value BIGINT (20); -- 学员编号 DECLARE pay_no_value BIGINT (20); -- 交费序号 DECLAR ...
- php 计算函数执行时间的方法及获得微妙的方法
// 获得微妙方法 function getMillisecond() { list($s1, $s2) = explode(' ', microtime()); return (float)spri ...
- Linux将命令添加到PATH中【转】
转自:http://www.jb51.net/LINUXjishu/150167.html 电脑中必不可少的就是操作系统.而Linux的发展非常迅速,有赶超微软的趋势.这里介绍Linux的知识,让你学 ...
- 编程中的runtime_error问题
前几天在编程中,代码如下: 头文件:ErrorHandlingModule.h //filename:ErrorHandlingModule.h #ifndef ErrorHandlingModule ...
- 非常好!!!---bash转义序列笔记---打印语句printf用法【转】
转自:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=2318684&fromuid=23571134 本教程由 惟吾无为 ...
- python 的requests如何使用代理
headers.py import random first_num = random.randint(55, 62) third_num = random.randint(0, 3200) four ...