leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

时间:2022-08-26 16:38:17

题目:

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 (根据前序和中序遍历构造二叉树)的更多相关文章

  1. LeetCode 105&period; Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C&plus;&plus;

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  2. LeetCode:105&lowbar;Construct Binary Tree from Preorder and Inorder Traversal &vert; 根据前序和中序遍历构建二叉树 &vert; Medium

    要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...

  3. &lbrack;Leetcode&rsqb; 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 ...

  4. 【LeetCode】105&period; Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  5. 105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树

    给定一棵树的前序遍历与中序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出前序遍历 = [3,9,20,15,7]中序遍历 = [9,3,15,20,7]返回如下的二叉树:    ...

  6. &lbrack;LeetCode&rsqb; 105&period; 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 ...

  7. &lpar;二叉树 递归&rpar; leetcode 105&period; 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 ...

  8. LeetCode 105&period; 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 ...

  9. 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 ...

随机推荐

  1. CBUUID UUIDString unrecognized selector sent to instance 错误

    CBUUID UUIDString unrecognized selector sent to instance 错误 ios7.0,4s 蓝牙出现上述错误! 查看api可知,错误原因,由于CBUUI ...

  2. A memory leak issue with WPF Command Binding

    Background In our application, we have a screen which hosts several tabs. In each tab, it contains a ...

  3. 微软TechEd2013大会将在北京、上海召开!

    微软TechEd2013大会将在北京.上海召开 大家期盼已久的微软TechEd2013大会终于到来了! 我公司依旧是微软公司指定票商 ,继续为您提供最最优质的售前咨询.最最完善的售后服务! 微软Tec ...

  4. Microsoft Anti-Cross Site Scripting Library V4&period;2 下载地址

    概述 微软反跨站脚本库V4.2(AntiXSS V4.2)是一种编码库,旨在帮助开发人员保护他们的ASP.NET基于Web的应用程序免受XSS攻击.它不同于编码库,因为它使用的白名单技术-有时也被称为 ...

  5. 修改placeholder的样式

    input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #666; } input:-moz-pl ...

  6. 使用Python做简单的字符串匹配

    由于需要在半结构化的文本数据中提取一些特定格式的字段.数据辅助挖掘分析工作,以往都是使用Matlab工具进行结构化数据处理的建模,matlab擅长矩阵处理.结构化数据的计算,Python具有与matl ...

  7. Git最牛最全详解

    阅读目录 Git是什么    SVN与Git的最主要的区别    在windows上如何安装Git    如何操作    创建版本库    把文件添加到版本库中    版本回退    理解工作区与暂存 ...

  8. bootstrap4 Reboot details summary 美化(点选禁止选中文本,单行隐藏角标,多行后移)

    bootstrap4 Reboot details summary 优化这块,主要是为了去掉details summary的角标~ 所以优化写了一下. 原始HTML <details> & ...

  9. &lbrack;转&rsqb;&lbrack;C&num;&rsqb;加密解密类

    { public static class Crypter { private static string FDefaultPassword = typeof(Crypter).FullName; p ...

  10. node的cookie-parser和express-session

    let express = require('express'); let cookieParser = require('cookie-parser'); let expressSession = ...