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 = [3,9,20,15,7]
inorder = [9,3,15,20,7]

Return the following binary tree:

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

4. Test cases

1) edge case

2)

preorder = [3,9,20,15,7]
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. day_4.30 py

    2018-4-30 13:02:32 ''' 多态:只有当调用方法的时候才知道调用父类 还是子类的方法(随变化而变化,等到真正实行的时候才知道结果) 面向对象三个特点: 封装 继承 多态 ''' cl ...

  2. Java学习者论坛【申明:来源于网络】

    学习者论坛[申明:来源于网络] 1.Java学习者论坛 2.51论坛 3.csdn论坛 4.JAVA ME论坛 地址|: http://www.javaxxz.com/ 地址|: http://bbs ...

  3. 使用ASP.NET Core的User Secrets特性

    昨天在一个集成测试项目中实际使用 ASP.NET Core 的 user secrets 保存敏感配置信息,避免了直接保存在 appsettings.json 中,在这篇随笔中记录一下. 使用 use ...

  4. windows下批量生成文件夹

    在windows环境下如果想要批量生成文件夹: 1.创建一个记事本文件 2.首行大写MD 3.后面加上你想创建的文件夹的名字,每个名字之间有空格 4.退出记事本并保存 5.将记事本文件后缀改为bat文 ...

  5. [No0000182]Parallel Programming with .NET-Partitioning in PLINQ

    Every PLINQ query that can be parallelized starts with the same step: partitioning.  Some queries ma ...

  6. [No0000116]SQLServer启用sa账户

    SQLServer如何启用sa账户,今天在这里唠叨一下关于SQL Server数据库如何启用sa账户的,作为一个数据库管理者,需要非常熟练掌握,具体步骤如下: 1.先登录数据库服务 首先在“开始”菜单 ...

  7. wpf数据绑定:xml数据绑定

    wpf中可以通过XmlDataProvider,来实现xml数据的绑定.它通过XmlDataProvider来绑定外部资源,需要命名一个 x:Key 值,以便数据绑定目标可对其进行引用,Source设 ...

  8. LeetCode 559 Maximum Depth of N-ary Tree 解题报告

    题目要求 Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  9. 机器学习:K-近邻算法

    K-近邻算法 优点:精度高.对异常值不敏感.无数据输入假定.缺点:计算复杂度高.空间复杂度高.使用数据范围:数值型和标称型. k-近邻算法的一般流程 搜集数据:可以使用任何方法.准备数据:距离计算所需 ...

  10. Java+selenium 如何操作日历控件

    场景:一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 但是,有的日期控件是readonly的 ,比如神 ...