Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

preorder = [,,,,]
inorder = [,,,,]

Return the following binary tree:

   / \

    /  \
      

前序、中序遍历得到二叉树,可以知道每一次前序新数组的第一个数为其根节点。在中序遍历中找到根节点对应下标,根结点左边为其左子树,根节点右边为其右子树,再根据中序数组中的左右子树个数,找到对应的前序数组中的左右子树新数组。设每次在中序数组中对应的位置为i,则对应的新数组为:

前序新数组:左子树[pleft+1,pleft+i-ileft],右子树[pleft+i-ileft+1,pright]

中序新数组:左子树[ileft,i-1],右子树[i+1,iright]  C++

 TreeNode* buildTree(vector<int>& preorder,int pleft,int pright,vector<int>& inorder,int ileft,int iright){
if(pleft>pright||ileft>iright)
return NULL;
int i=;
for(i=ileft;i<=iright;i++){
if(preorder[pleft]==inorder[i])
break;
}
TreeNode* cur=new TreeNode(preorder[pleft]);
cur->left=buildTree(preorder,pleft+,pleft+i-ileft,inorder,ileft,i-);
cur->right=buildTree(preorder,pleft+i-ileft+,pright,inorder,i+,iright);
return cur;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
return buildTree(preorder,,preorder.size()-,inorder,,inorder.size()-);
}

LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++的更多相关文章

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

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

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

  3. LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium

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

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

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

  5. [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 ...

  6. [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 ...

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

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

  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. springmvc java配置

    配置DispatcherServlet DispatcherServlet的是SpringMVC的核心.在这里请求会第一次接触都框架,它要负责将请求路由到其他的组件之中. 使用Java配置将Dispa ...

  2. tornado--输入和输出

    tornado--输入和输出 tornado的self.write只接受byte,Unicode,dict三种格式的对象. self.write会存在一个缓冲区,当不强制断开缓冲的时候,它会把当前函数 ...

  3. hbase整合

    hbase與hive整合    1. hive中有數據 --> 創建hive管理表映射hbase    例如:        1)hive創建內部表            create tabl ...

  4. django 分页组件

      一.仿django分页功能自己实现 urls.py 1 2 3 4 5 6 7 8 9 from django.conf.urls import url from django.contrib i ...

  5. Hello_World

    简单A+B #include <stdio> int main() { int a,b; scanf("%d%d",&a, &b); printf(&q ...

  6. MySQL1-安装

    MySQL 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以将数据 ...

  7. Kuberneteser二进制安装与配置(二)

    环境:Centos7 版本:Kubernetes v1.11.4   一.下载Kubernetes   1)下载 wget https://github.com/kubernetes/kubernet ...

  8. zabbix 监控第三方网络

    背景 业务需要到一些第三方网络取数据,有时业务不能用,就需要跑到服务器上telnet第三方端口,排查是否网络问题.所以需要一个检测第三方端口的监控. net.tcp.service[service,& ...

  9. LOJ 2547 「JSOI2018」防御网络——思路+环DP

    题目:https://loj.ac/problem/2547 一条树边 cr->v 会被计算 ( n-siz[v] ) * siz[v] 次.一条环边会被计算几次呢?于是去写了斯坦纳树. #in ...

  10. 利用工具将数据库中的表导出到word中

    1.动软代码生成器 效果图: 数据库设计说明书中的一项,刚好我负责写这个文档, 18张表,前两张表是自己画表格自己填充内容,写到第三张表的时候就已经崩溃了(我觉得我耐力还是够的,怎么说也画完了两张表呢 ...