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

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

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

/*** 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) {return buildTree(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);}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 = 0;for (i = iLeft; i <= iRight; ++i) {if (preorder[pLeft] == inorder[i]) break;}TreeNode *cur = new TreeNode(preorder[pLeft]);cur->left = buildTree(preorder, pLeft + 1, pLeft + i - iLeft, inorder, iLeft, i - 1);cur->right = buildTree(preorder, pLeft + i - iLeft + 1, pRight, inorder, i + 1, iRight);return cur;}
};

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

Preorder:    5  4  11  8  13  9

Inorder:    11  4  5  13  8  9

5  4  11  8  13  9      =>          5

11  4  5  13  8  9                /  \

4  11     8   13  9      =>         5

11  4     13  8  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的五棵二叉树,它们的中序遍历都不相同,而它们的后序遍历却有相同的,所以只有和中序遍历一起才能唯一的确定一棵二叉树。

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

转载于:https://www.cnblogs.com/grandyang/p/4296500.html

[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树...相关推荐

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

  2. [leetcode] Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

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

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

  5. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...

  6. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...

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

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

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

最新文章

  1. 排名前 16 的 Java 工具类,哪个你没用过?
  2. js 中 setInterval 的返回值问题
  3. 奇妙的棋盘(建图+搜索)
  4. VC++6.0和VC++2010的区别
  5. mysql 8.0 创建函数_MySQL 8.0 新增特性
  6. 服务器t4卡在哪个位置,英特尔(Intel )X710-T4融合网络适配器4口万兆X710T4服务器网卡...
  7. 儿子获奖发明和父亲研究所成果高度相似,还不止一个
  8. linux 常用SHELL
  9. 蓝桥杯2015 C语言大学B组 C/C++
  10. 为什么选择Netty作为基础通信框架?
  11. 读书笔记之《网络是怎样连接的》
  12. EnergyPlus笔记
  13. mini6410 LED驱动程序及LED测试程序的设计
  14. 服务器系统做个备份吗,服务器操作系统能做备份吗
  15. js实现夜空 干货比较多
  16. 生活时尚酒店品牌JOJOE进军中国,首批选址7个城市,未来将开设至少1300家门店 | 美通社头条...
  17. Archlinux 安装、美化、软件入门(四)
  18. win10实现debug
  19. 数字货币的路在哪里?
  20. 如何在PowerPoint演示文稿中突出显示文本

热门文章

  1. 如何快速调出软键盘_*小星推荐*—如何快速的制作模具3D装配档
  2. php 详情页,人物详情页.php
  3. Linux内核分析考试试题,linux内核分析第二周作业
  4. iframe框架_性能优化去除iframe脚手架升级方案
  5. 为什么我的眼里常含泪水,因为Mysql让我变的深沉(2021最新版mysql安装)
  6. C语言:用二维字符数组的每行存储键盘输入的字符串,将这些字符串按字典顺序升序排序,输出排序后的结果。
  7. C语言:编写程序,打开文本文件stu.txt,读出文件内容,将其中的字符‘s’删除,将修改后的内容存到student.txt文件中。
  8. android 连续调用方法是,android – SwitchPreferences多次调用onPreferenceChange()方法
  9. 【神经网络与深度学习摘要】第1章 绪论
  10. Hbase Scan类 ResultScanner类