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

  1. preorder = [3,9,20,15,7]
  2. inorder = [9,3,15,20,7]

Return the following binary tree:

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

这道题要求用先序和中序遍历来建立二叉树,跟之前那道 Construct Binary Tree from Inorder and Postorder Traversal 原理基本相同,针对这道题,由于先序的顺序的第一个肯定是根,所以原二叉树的根节点可以知道,题目中给了一个很关键的条件就是树中没有相同元素,有了这个条件就可以在中序遍历中也定位出根节点的位置,并以根节点的位置将中序遍历拆分为左右两个部分,分别对其递归调用原函数,参见代码如下:

  1. class Solution {
  2. public:
  3. TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
  4. return buildTree(preorder, , preorder.size() - , inorder, , inorder.size() - );
  5. }
  6. TreeNode *buildTree(vector<int> &preorder, int pLeft, int pRight, vector<int> &inorder, int iLeft, int iRight) {
  7. if (pLeft > pRight || iLeft > iRight) return NULL;
  8. int i = ;
  9. for (i = iLeft; i <= iRight; ++i) {
  10. if (preorder[pLeft] == inorder[i]) break;
  11. }
  12. TreeNode *cur = new TreeNode(preorder[pLeft]);
  13. cur->left = buildTree(preorder, pLeft + , pLeft + i - iLeft, inorder, iLeft, i - );
  14. cur->right = buildTree(preorder, pLeft + i - iLeft + , pRight, inorder, i + , iRight);
  15. return cur;
  16. }
  17. };

下面来看一个例子, 某一二叉树的中序和后序遍历分别为:

Preorder:    5  4  11  8  13  9

Inorder:    11  4  5  13  8  9

  4  11  8  13  9      =>          5

11  4    13  8  9                /  \

  11        13  9      =>         5

11       13    9                  /  \

                             4   8

11       13    9        =>         5

11       13    9                    /  \

                             4   8

                            /    /     \

                           11    13    9

做完这道题后,大多人可能会有个疑问,怎么没有由先序和后序遍历建立二叉树呢,这是因为先序和后序遍历不能唯一的确定一个二叉树,比如下面五棵树:

1      preorder:    1  2  3
   / \       inorder:       2  1  3
 2    3       postorder:   2  3  1

1       preorder:     1  2  3
      /       inorder:       3  2  1
    2          postorder:   3  2  1
   /
 3

1        preorder:    1  2  3
      /        inorder:      2  3  1
    2       postorder:  3  2  1
      \
       3

1         preorder:    1  2  3
         \        inorder:      1  3  2
          2      postorder:  3  2  1
         /
       3

1         preorder:    1  2  3
         \      inorder:      1  2  3
          2      postorder:  3  2  1
            \
    3

从上面我们可以看出,对于先序遍历都为 1 2 3 的五棵二叉树,它们的中序遍历都不相同,而它们的后序遍历却有相同的,所以只有和中序遍历一起才能唯一的确定一棵二叉树。但可能会有小伙伴指出,那第 889 题 Construct Binary Tree from Preorder and Postorder Traversal 不就是从先序和后序重建二叉树么?难道博主被啪啪打脸了么?难道博主的一世英名就此毁于一旦了么?不,博主向命运的不公说不,请仔细看那道题的要求 "Return any binary tree that matches the given preorder and postorder traversals.",是让返回任意一棵二叉树即可,所以这跟博主的结论并不矛盾。长舒一口气,博主的晚节保住了~

Github 同步地址:

https://github.com/grandyang/leetcode/issues/105

类似题目:

Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Preorder and Postorder Traversal

参考资料:

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/34538/My-Accepted-Java-Solution

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/34562/Sharing-my-straightforward-recursive-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

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

  1. (二叉树 递归) 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 ...

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

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

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

  5. Java for 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 ...

  6. [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)

    原题 题意: 根据先序和中序得到二叉树(假设无重复数字) 思路: 先手写一次转换过程,得到思路. 即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root ...

  7. LeetCode OJ: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

    原题地址 基本二叉树操作. O[       ][              ] [       ]O[              ] 代码: TreeNode *restore(vector< ...

  9. leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树

    不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...

随机推荐

  1. 【shell脚本】批量修改扩展名===modifyExtension.sh

    前提:需切换到需要批量修改扩展名的目录下,运行脚本 [root@VM_0_10_centos shellScript]# cat modifyExtension.sh #!/bin/bash # 编写 ...

  2. svg描边路径动画

    svg描边路径动画<pre><!DOCTYPE html><html> <head> <meta charset="UTF-8" ...

  3. 【前端知识体系-JS相关】ES6专题系列总结

    1.如何搭建ES6的webpack开发环境? 安装Node环境 node -v // 10.14.1 安装NPM环境 npm -v // 6.4.1 安装babel npm install @babe ...

  4. redis命令之 ----List(列表)

    BLPOP BRPOP BRPOPLPUSH LINDEX LINDEX key index 返回列表 key 中,下标为 index 的元素. 下标(index)参数 start 和 stop 都以 ...

  5. golang两种在for循环中使用goroutine的错误形式

    1. 闭包中使用循环体中变化的量 platground链接: https://play.golang.org/p/6x6_tuQNjUO type Value struct{ val int } fu ...

  6. Centos7编译安装Nginx+keepalived

    一.安装环境.主机信息及软件版本 Nginx:1.12.2keepalived:2.0.12时间同步(同步后确认各服务器时间是否一致,不一致需要修改一下时区) 关闭防火墙 二.编译安装Nginx 1. ...

  7. Java面试- JVM 内存模型讲解

    经常有人会有这么一个疑惑,难道 Java 开发就一定要懂得 JVM 的原理吗?我不懂 JVM ,但我照样可以开发.确实,但如果懂得了 JVM ,可以让你在技术的这条路上走的更远一些. JVM 的重要性 ...

  8. Tomcat 简单容器化

    Tomcat 容器化 思考 问题1 , Tomcat 容器化,Tomcat 如何配置 APR 连接器 Tomcat 的基础镜像已经是开启了 APR. 问题2, Tomcat 是每次都需要重新构建. 一 ...

  9. 2019-11-29-win10-uwp-列表模板选择器

    原文:2019-11-29-win10-uwp-列表模板选择器 title author date CreateTime categories win10 uwp 列表模板选择器 lindexi 20 ...

  10. Ubuntu Err:1 http://us.archive.ubuntu.com/ubuntu bionic InRelease Could not resolve 'us.archive.ubuntu.com' 错误

    Ubuntu 更新 apt-get update 的时候 出现 Err: http://us.archive.ubuntu.com/ubuntu bionic InRelease Could not ...