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 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
解题思路
- 根据前序数组可确定根节点;
- 根据中序数组可确定左右节点的划分;
- 重复此过程。
# 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相关推荐
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 本博客转载自:http://www.cnblogs.co ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- [leetcode] Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [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 ...
最新文章
- 用 Spark 为 Elasticsearch 导入搜索数据
- python读取本地文件-python解析本地HTML文件
- Python安装PyOpenGL
- wpf控件设计时支持(3)
- 易思汇完成近亿元B轮融资,信中利投资
- Linux rescue救援(光盘修复)模式详解
- JAVA数组批量设值(初始化)的办法
- ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
- 二阶滤波器原理及算法程序
- SELECT 1 FROM DUAL中的DUAL的作用
- oracle order siblings by,sql中ORDER SIBLINGS BY排序的含义
- isb 汇编_DSB,ISB,DMB指令 | 学步园
- 做抖音有哪些变现方式
- 使用RocketMQ进行银行转账
- U3d 代码修改图片的尺寸
- 计算机机房年度重点工作,信息中心计算机的机房建设要求最新.doc
- c++模板:typename的使用
- 2.4万余门课程在线学,22家平台面向全国高校开放
- vue 中报错 [Element Migrating][ElDialog][Attribute]: size is removed.
- oracle 磁带 恢复,关于磁带库rman备份恢复的案例
热门文章
- 智能家庭本周锋闻:式微的三星和老气的SmartThings
- 全球40起黑客攻击或与CIA有关 长角牛黑客组织打卡“上班”
- Invalid input for operation: physical_network 'physnet1' unknown for flat provider network.
- MySQL server PID file could not be found!
- nagios 监控NFS
- 我知道的一些 ”运行“ 窗体下的命令,个人使用
- Windows Home Server 2011 RC 安装体验
- [盘点]从《行者无疆》开始了解欧洲
- 圆周率派在电脑上怎样输入_Mac电脑上FCPX软件怎样导入3D LUT调色预设
- JVM 方法区 ORACLE官方文档