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. jquery库的cookie用法

    一个完整设置与读取cookie的页面代码: <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  2. JavaScript对象(第四天)

    面向对象编程中,JavaScript并不完全具备封装.继承.多态:在JavaScript中,对象是一个无序的键值对集合 封装JavaScript是具备的,将属性和方法定义到对象内部: 继承,在java ...

  3. 企业SQL笔试

    Student(SID, Sname, Sage, Ssex) 学生表        Course(CID, Cname, TID) 课程表        SC(SID, CID, score) 成绩 ...

  4. Go 包管理工具--glide

    网上有一篇解释glide比较好的文章:https://my.oschina.net/u/553243/blog/1475626 在命令行中输入glide $glide NAME: glide - Ve ...

  5. mnist的格式说明,以及在python3.x和python 2.x读取mnist数据集的不同

    有一个关于mnist的一个事例可以参考,我觉得写的很好:http://www.cnblogs.com/x1957/archive/2012/06/02/2531503.html #!/usr/bin/ ...

  6. Java多线程学习(总结很详细!!!)

    https://www.cnblogs.com/yjd_hycf_space/p/7526608.html

  7. Mysql ssl 连接

    在Azure创建了一个Mysql5.7服务,因为默认使用ssl连接,需要下载Azure的证书,并使用openssl生成客户端的证书.具体流程参考官方文档 大致步骤: 下载根证书, 安装openssl, ...

  8. [转]Python机器学习笔记 异常点检测算法——Isolation Forest

    Isolation,意为孤立/隔离,是名词,其动词为isolate,forest是森林,合起来就是“孤立森林”了,也有叫“独异森林”,好像并没有统一的中文叫法.可能大家都习惯用其英文的名字isolat ...

  9. 关于Asset Library核心功能的一些计划

    Asset Library是公司计划中的一个网站,用于存放图像,视频,三维文件等资产,在之前的一个多月中我写完了该网站的后台及部分前端,现在就剩最后一部分了,这也是最棘手最核心的部分,就是在网页上快速 ...

  10. Java8-Optional与null

    对null进行处理 程序中经常需要对null情况进行处理,比如Course类中有一个List stuList属性,Student有一个name属性. 现在想要查看某个student的name属性的长度 ...