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
  6.  
  7. 这个题目的思路就是利用preorder inorder的两种特性, 我们可以发现, preorder[0] 总是root, 然后inorder 里面根据之前的root, 可以将inorder分为left treeright tree,
    然后return root, recursive call即可.
  8.  
  9. 1. Constraints
    1) pre or in can be empty
  10.  
  11. 2. ideas
    recursively DFS, 分别得到root, root.left & root.right, 返回root
  12.  
  13. 3. Code
  1. class Solution:
  2. def constructBT(self, preorder, inorder):
  3. if not preorder or not inorder: return #note 别忘了not before inorder
  4. root, index = TreeNode(preorder[0]), inorder.index(preorder[0])
  5. root.left = self.constructBT(preorder[1:1+index], inorder[:index])
  6. root.right = self.constructBT(preorder[1+index:], inorder[index+1:])
  7. return root

4. Test cases

1) edge case

2)

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

[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal的更多相关文章

  1. [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal

    Given inorder and postorder 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 由先序和中序遍历建立二叉树

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

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

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

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

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

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

  9. Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal

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

随机推荐

  1. C - Monthly Expense

    Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...

  2. python爬虫重定向次数过多问题

    错误提示如下: raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects, response=resp)requests ...

  3. Redis 应该是存放的数据超出了范围

    使用ServiceStack操作Redis 发生了 System.StackOverflowException HResult=0x800703E9 Source=<无法计算异常源> St ...

  4. 洛谷P1147 连续自然数和【二分】

    题目:https://www.luogu.org/problemnew/show/P1147 题意: 给定一个数m,问有多少个数对$(i,j)$,使得$i$到$j$区间的所有整数之和为m.输出所有的解 ...

  5. PHP利用反射根据类名反向寻找类所在文件

    有时候分析源码时,会被博大精深的层层代码搞得晕头转向,不知道类是定义在哪个文件里的,有时候IDE所提供的方法声明未必准确.在这种情况下,我们可以利用反射找到类所在的文件. 在你发现实例化类的地方(例如 ...

  6. python 过滤掉字符串中的回车符与换行符(\t\n)

    我们在文本数据预处理前,要将数据统一整理成需要的格式,其中有回车(\t)或者(\n)符号,会对我们的数据保存有影响,那么就需要将其过滤掉. 比较简单的方法,用replace()将这些符号替换为空,一定 ...

  7. React 入门实例

    React 入门实例教程 一.安装 React 的安装包,可以到官网下载. $ git clone git@github.com:ruanyf/react-demos.git 如果你没安装 git, ...

  8. typeof(), __typeof(), __typeof__(), -isKindOfClass:的区别

    关于  typeof()和 __typeof()  和 __typeof__() ,Stack Overflow 有一段解释,大概意思是, __typeof() .__typeof__() 是C语言的 ...

  9. linux虚拟机设置本地yum源

    1.挂载ISO镜像 2.创建文件夹,用于挂载光盘,mkdir /mnt/cdrom mount /dev/cdrom /mnt/cdrom 3.修改 repo 文件 baseurl=file:///挂 ...

  10. HTTP协议之Transfer-Encoding

    HTTP协议中的Transfer-Encoding 浏览器和服务器端支持持久连接 持久连接(Persist Connection) HTTP1.0默认不是持久连接的 HTTP1.1默认是持久连接的 在 ...