Problem Link:

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

The basic idea is same to that for Construct Binary Tree from Inorder and Postorder Traversal. We solve it using a recursive function. First, we find preorder[0] in inorder, let's say inorder[i] == preorder, then construct the left tree from preorder[1..i] and inorder[0..i-1] and the right tree from preorder[i+1..n-1] and inorder[i+1..n-1].

The code is as follows.

  1. # Definition for a binary tree node
  2. # class TreeNode:
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7.  
  8. class Solution:
  9. # @param preorder, a list of integers
  10. # @param inorder, a list of integers
  11. # @return a tree node
  12. def buildTree(self, preorder, inorder):
  13. n = len(preorder)
  14. if n == 0:
  15. return None
  16. elif n == 1:
  17. return TreeNode(preorder[0])
  18. else:
  19. mid_inorder = inorder.index(preorder[0])
  20. root = TreeNode(preorder[0])
  21. root.left = self.buildTree(preorder[1:mid_inorder+1], inorder[:mid_inorder])
  22. root.right = self.buildTree(preorder[mid_inorder+1:], inorder[mid_inorder+1:])
  23. return root

【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

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

  3. 【Leetcode】【Medium】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刷题笔记】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】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. 【树】Construct Binary Tree from Preorder and Inorder Traversal

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后 ...

  7. 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...

  8. 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★

    1.题目 2.思路 3.java代码 //测试 public class BuildTreeUsingInorderAndPreorder { public static void main(Stri ...

  9. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal

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

随机推荐

  1. python 异常处理、文件常用操作

    异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm

  2. php简单单例模式

    所谓单例模式,适用于使用一个对象可以完成所有的业务逻辑的类(一般不考虑继承的类) //单例模式 function getInstance($class_name){ //创建一个存储各种需要单例的类的 ...

  3. 艺萌文件上传下载及自动更新系统(基于networkComms开源TCP通信框架)

    1.艺萌文件上传下载及自动更新系统,基于Winform技术,采用CS架构,开发工具为vs2010,.net2.0版本(可以很容易升级为3.5和4.0版本)开发语言c#. 本系统主要帮助客户学习基于TC ...

  4. The type String cannot be constructed. You must configure the container to supply this value.

    利用 Enterprise Library 5.0 Microsoft.Practices.EnterpriseLibrary.Common Microsoft.Practices.Enterpris ...

  5. Linux版Matlab R2015b的bug——脚本运行的陷阱(未解决)

    0 系统+软件版本 系统:CentOS 6.7 x64, 内核 2.6.32-573.el6.x86_64软件:Matlab R2015b(包括威锋网和东北大学ipv6下载的资源,都测试过) 1 脚本 ...

  6. java动手动脑和课后实验型问题String类型

    1.请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? true true false 总结: 使用new关键字创建字符串对象时, 每次申请 ...

  7. 非Spring下的Quartz

    转自:Nick Huang.    http://www.cnblogs.com/nick-huang/ 阅读目录 > 参考的优秀资料 > 版本说明 > 简单的搭建 > 在We ...

  8. <java基础学习>RE 基础语法

    public class MyFirstJavaProgram{ public static void main(String[] args ){ System.out.println("H ...

  9. sql之多表连接

    最近遇到特别多多表连接的问题,因此随笔记下,开始学java和mysql的时间太短,有见解不周的地方,希望读者可以提出探讨. 对于left join.right join和inner join(join ...

  10. Oracle 添加第二个实例 和 监听

    1.添加第二个实例 配置和移植工具 -->Database Configuration Assistant 然后一步一步创建实例(实例名为ORCL2) 创建成功后会在系统的服务里面有个服务名称为 ...