[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 duplicates do not exist in the tree.
For example, given
inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]
Return the following binary tree:
3
/ \
9 20
/ \
15 7
这个题目思路跟[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal一样, 只是root是postorder[-1], 而inorder[0] 而已, 本质一样.
Code:
class Solution:
def buildTree(self, postorder, inorder):
if not postorder or not inorder: return
root, index = TreeNode(postorder[-1]), inorder.index(postorder[-1])
root.left = self.buildTree(postorder[:index], inorder[:index])
root.right = self.buildTree(postorder[index: -1], inorder[index+1:])
return root
[LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal的更多相关文章
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)
原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...
- leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal----- java
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- solus 系统 - 安装 mariadb
# 安装 mariadb(这个系统没有 mysql 库) $ sudo eopkg it mariadb $ sudo eopkg it mariadb-server # 初始化数据文件地址 $ su ...
- Spring实战 难懂的JavaBean
bean中文解释为:豆; 豆形种子; 毫无价值的东西. 按照上面的意思,很难理解Bean是个什么鬼,Java豆? 我们先来看一个典型的JavaBean,直观地理解下: public class Per ...
- API(二)之Requests and Responses
这一部分可以让返回的api信息以两种格式呈现:.json和.api REST框架的核心部分.我们来介绍几个基本的组成部分. Request objects REST框架引入了Request对象来扩展常 ...
- .Net Windows Service(服务) 调试安装及System.Timers.Timer 使用
Windows Service(服务) 是运行在后台的进程 1.VS建立 Windows 服务(.NET Framework) 2.添加Timer 双击Service1.cs可以拖控件(System ...
- 关于ti环境搭建 IAR 为什么IAR不能跳转的真正原因
1.IAR工程莫名其妙的错误,可以通过复制工程文件来解决 2.cc2540环境配置,尽量默认到c盘,以便解决不必要的麻烦,另外,不同的库尽量安装对应的环境文件,比如freertos等环境 3.对于蓝牙 ...
- [No0000F9]C# 运算符重载
您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的.与其他函数一样,重载运 ...
- MAC apache服务器搭建
一.启动原本服务器 首先打开“终端(terminal)”,输入 sudo apachectl -v,(可能需要输入机器秘密).如下显示Apache的版本: 可以输入启动命令进行启动: sudo apa ...
- json和pickle,XML
什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻量级的文本数据交换格式 JSON 独立于语言:JSON ...
- MonkeyRunner_运行脚本(一)
前提:环境已部署, 使用数据线连接上真机 一.使用cmd窗口单步执行monkeyrunner命令 打开cmd窗口,输入monkeyrunner (前提设置好环境变量):然后按照monkeyrunne ...
- idea+maven+springboot+mybatis+springmvc+shiro
springboot就是把创建项目简单化,省去了以往的配置mybatis.springmvc的繁琐过程. 搭建web应用三个主要功能,请求和响应,数据库交互,权限配置. 一.idea创建项目 (1) ...