转载自:http://blog.csdn.net/sbitswc/article/details/26433051

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

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

There is an example.
        _______7______/              \__10__          ___2/      \        /4       3      _8\    /1  11
The preorder and inorder traversals for the binary tree above is:
preorder = {7,10,4,3,1,2,8,11}
inorder = {4,10,3,1,7,11,8,2}

The first node in preorder alwasy the root of the tree. We can break the tree like:
1st round:
preorder:  {7}, {10,4,3,1}, {2,8,11}
inorder:     {4,10,3,1}, {7}, {11, 8,2}

        _______7______/              \{4,10,3,1}       {11,8,2}
Since we alreay find that {7} will be the root, and in "inorder" sert, all the data in the left of {7} will construct the left sub-tree. And the right part will construct a right sub-tree. We can the left and right part agin based on the preorder.

2nd round
left part                                                                            right part
preorder: {10}, {4}, {3,1}                                              {2}, {8,11}
inorder:  {4}, {10}, {3,1}                                                {11,8}, {2}

        _______7______/              \__10__          ___2/      \        /4      {3,1}   {11,8}
see that, {10} will be the root of left-sub-tree and {2} will be the root of right-sub-tree.

Same way to split {3,1} and {11,8}, yo will get the complete tree now.

        _______7______/              \__10__          ___2/      \        /4       3      _8\    /1  11
So, simulate this process from bottom to top with recursion as following code.

c++

[cpp] view plain copy
  1. TreeNode *BuildTreePI(
  2. vector<int> &preorder,
  3. vector<int> &inorder,
  4. int p_s, int p_e,
  5. int i_s, int i_e){
  6. if(p_s > p_e) return NULL;
  7. int pivot = preorder[p_s];
  8. int i = i_s;
  9. for(;i<i_e;i++){
  10. if(inorder[i] == pivot)
  11. break;
  12. }
  13. int length1 = i-i_s-1;
  14. int length2 = i_e-i-1;
  15. TreeNode* node = new TreeNode(pivot);
  16. node->left = BuildTreePI(preorder,inorder,p_s+1,length1+p_s+1,i_s, i-1);
  17. node->right = BuildTreePI(preorder, inorder, p_e-length2, p_e, i+1, i_e);
  18. return node;
  19. }
  20. TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
  21. return BuildTreePI(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);
  22. }

java

[java] view plain copy
  1. public TreeNode buildTree(int[] preorder, int[] inorder) {
  2. return buildPI(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);
  3. }
  4. public TreeNode buildPI(int[] preorder, int[] inorder, int p_s, int p_e, int i_s, int i_e){
  5. if(p_s>p_e)
  6. return null;
  7. int pivot = preorder[p_s];
  8. int i = i_s;
  9. for(;i<i_e;i++){
  10. if(inorder[i]==pivot)
  11. break;
  12. }
  13. TreeNode node = new TreeNode(pivot);
  14. int lenLeft = i-i_s;
  15. node.left = buildPI(preorder, inorder, p_s+1, p_s+lenLeft, i_s, i-1);
  16. node.right = buildPI(preorder, inorder, p_s+lenLeft+1, p_e, i+1, i_e);
  17. return node;
  18. }

按照前序遍历和中序遍历构建二叉树相关推荐

  1. 通过前序遍历和中序遍历构建二叉树 python实现

    前言 通过前序遍历和中序遍历构建二叉树的原理,主要是找前序遍历根节点在中序遍历中的位置,然后将二叉树而成左子树和右子树,然后依次进行这样的操作,思路还是比较简单的 代码 class Node:def ...

  2. java根据前序和中序建树_Java实现根据前序遍历构建二叉树(前序遍历、中序遍历、后序遍历)...

    Java实现根据前序遍历构建二叉树(前序遍历.中序遍历.后序遍历),Java关于ACM的代码真的好少,想参考如何用java实现二叉树googl 前言 Java关于ACM的代码真的好少,想参考如何用ja ...

  3. 根据二叉树前序遍历和中序遍历重建二叉树

    剑指 Offer 07. 重建二叉树 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字. 例如,给出 前序遍历 preorder = [3, ...

  4. 根据前序遍历和[中序遍历]

    根据前序遍历和[中序遍历] 1. 递归法: 先序遍历:根节点→左子树→右子树. 中序遍历:左子树→根节点→右子树. 后续遍历:左子树→右子树→根节点. 根据前序遍历和中序遍历建立二叉树,根据以上性质可 ...

  5. 根据前序遍历和中序遍历创建二叉树

    根据前序遍历和中序遍历创建二叉树 题目要求如下: 给定某一个二叉树的前序遍历和中序遍历,要求据此创建一颗符合这样遍历顺序的二叉树. 前序遍历和中序遍历的概念以及特性: 前序遍历:先遍历节点本身,再遍历 ...

  6. python实现二叉树的重建1 之由前序遍历和中序遍历重建

    前言 此题是关于树的面试题目的常见题型,题目的含义很清晰,这个就不用多说了 解法 关于这道题的解法有很多不同的样式,通用的解法是这样的: 假如现在我们有如下两个遍历的情况 preorder: [1, ...

  7. 根据二叉树的前序遍历和中序遍历重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  8. python实现二叉树遍历(前序遍历、中序遍历、后序遍历)

    python实现二叉树遍历(前序遍历.中序遍历.后序遍历) 在计算机科学中,二叉树是一种树数据结构,其中每个节点最多有两个子节点,称为左子节点和右子节点.使用集合理论概念的递归定义是(非空)二叉树是元 ...

  9. 前序遍历与中序遍历确定后序遍历

    1003. 二叉树后序遍历     Total: 137 Accepted: 80                 Time Limit: 3sec    Memory Limit:256MB Des ...

  10. 二叉树的前序遍历,中序遍历,后序遍历学习 (原)

    经验: 不要死记各个遍历节点的位置,将一个复杂的二叉树当作一个个小的二叉树学习前序遍历,中序遍历,后序遍历会更容易理解 转载于:https://www.cnblogs.com/gyrgyr/p/962 ...

最新文章

  1. 【C#】【Log】Common.Logging.MultipleLogger及ETWLoggerd研究
  2. svchost.exe介绍
  3. 在SAP WebClient UI里显示倒数计时的UI
  4. 22 FI配置-财务会计-定义收益留存科目(Retained Earning Account)
  5. 前端- 不用React 而使用 Vue,这么做对吗?
  6. JAVA 中 Redis与ehcache对比与使用
  7. 基于Vue2.0+Vuex+Axios+NodeJs+Express+MySQL实现京东移动web商城
  8. Qt - 自定义窗口 点击窗口外自动关闭
  9. 【英语阅读】经济学人 | 人脸识别不只是另一种技术。它将改变社会
  10. 企业微信机器人WorkTool使用文档
  11. 【并发编程】Once 基本用法和如何实现以及常见错误
  12. 华为nova6se怎么升级鸿蒙,华为EMUI11支持哪些手机
  13. Mr.Alright---安卓N系统最近任务锁定功能实现
  14. 什么是计算机语言????
  15. Gunicorn-使用详解
  16. Java jdk14.0.1安装简单步骤
  17. 【目标检测数据集汇总】YOLO txt格式各种数据集
  18. XDU Problem 1037 - 智破机枪阵
  19. 如何给linux添加一个dns服务器记录,在windows dns服务器中添加SRV记录
  20. 小米路由器老毛子固件aria2配置

热门文章

  1. 《iVX 高仿美团APP制作移动端完整项目》03 推介信息及推荐商家分析及制作
  2. CCF-CSP认证201312-1(出现次数最多的数)
  3. 现在的男生真的太惨了
  4. 过了双十一之后的你。。| 今日最佳
  5. 等我敲完这行代码,就和你离婚!
  6. 数据挖掘的十种分析方法
  7. 计算机如何实现共享接入,局域网内电脑实现共享设置方法
  8. 2018年最受大家欢迎的五大机器学习工具和五大数据学习工具
  9. oracle+查表物理块数,如何统计一段时间内 发生在某个表上的 物理读写的块数
  10. python怎么安装开发版_python - easy_install的安装和使用