题目

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

解题思路

  1. 根据前序数组可确定根节点;
  2. 根据中序数组可确定左右节点的划分;
  3. 重复此过程。
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:if inorder:root_val = preorder.pop(0)ind = inorder.index(root_val)root = TreeNode(root_val)root.left = self.buildTree(preorder, inorder[0:ind])root.right = self.buildTree(preorder, inorder[ind+1:])return root

LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal相关推荐

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

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

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

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

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

  4. LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal-前序中序遍历构造二叉树-Python和Java递归解法

    题目地址:Construct Binary Tree from Preorder and Inorder Traversal - LeetCode Given preorder and inorder ...

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

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

  7. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

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

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

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

最新文章

  1. 用 Spark 为 Elasticsearch 导入搜索数据
  2. python读取本地文件-python解析本地HTML文件
  3. Python安装PyOpenGL
  4. wpf控件设计时支持(3)
  5. 易思汇完成近亿元B轮融资,信中利投资
  6. Linux rescue救援(光盘修复)模式详解
  7. JAVA数组批量设值(初始化)的办法
  8. ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
  9. 二阶滤波器原理及算法程序
  10. SELECT 1 FROM DUAL中的DUAL的作用
  11. oracle order siblings by,sql中ORDER SIBLINGS BY排序的含义
  12. isb 汇编_DSB,ISB,DMB指令 | 学步园
  13. 做抖音有哪些变现方式
  14. 使用RocketMQ进行银行转账
  15. U3d 代码修改图片的尺寸
  16. 计算机机房年度重点工作,信息中心计算机的机房建设要求最新.doc
  17. c++模板:typename的使用
  18. 2.4万余门课程在线学,22家平台面向全国高校开放
  19. vue 中报错 [Element Migrating][ElDialog][Attribute]: size is removed.
  20. oracle 磁带 恢复,关于磁带库rman备份恢复的案例

热门文章

  1. 智能家庭本周锋闻:式微的三星和老气的SmartThings
  2. 全球40起黑客攻击或与CIA有关 长角牛黑客组织打卡“上班”
  3. Invalid input for operation: physical_network 'physnet1' unknown for flat provider network.
  4. MySQL server PID file could not be found!
  5. nagios 监控NFS
  6. 我知道的一些 ”运行“ 窗体下的命令,个人使用
  7. Windows Home Server 2011 RC 安装体验
  8. [盘点]从《行者无疆》开始了解欧洲
  9. 圆周率派在电脑上怎样输入_Mac电脑上FCPX软件怎样导入3D LUT调色预设
  10. JVM 方法区 ORACLE官方文档