[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 ...
随机推荐
- logback logback.xml常用配置详解(一)<configuration> and <logger>
logback logback.xml常用配置详解(一)<configuration> and <logger> 博客分类: Log java loglogback 原创文章 ...
- HTML5:基本语句
HTML5:超文本标记语言(HTML)第五次重大修改 我用的sulime编辑器,用Package contorl后安装emmet插件来编辑HTML代码,以下为sublime操作小技巧 ctrl+s 保 ...
- Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案
使用Newtonsoft.Json 转换DateTime类型时,若使用标准转换,则字符串内会有一个T(虽然再转换成DateTime没有问题). 若要转换成DateTime没有T,可以加上特性: pub ...
- python摸爬滚打之day010----函数进阶
1.函数动态传参 *args : 将所有的位置参数打包成一个元组的形式. **kwargs : 将所有的关键字参数打包成一个字典的形式. 形参的接收顺序: 位置参数 > *args > ...
- Java如何对List集合的操作方法(一)
目录: list中添加,获取,删除元素: list中是否包含某个元素: list中根据索引将元素数值改变(替换): list中查看(判断)元素的索引: 根据元素索引位置进行的判断: 利用list中索引 ...
- 【pyqtgraph】pyqtgraph-鼠标互动
pyqtgraph绘图库官方文档学习-鼠标互动(mouse interaction) 鼠标互动 大多数使用pyqtgraph数据可视化的应用程序都会生成可以使用鼠标进行交互式缩放,平移和配置的小部件. ...
- webpack浅析~
1.webpack打包原理: 把所有依赖打包成一个 bundle.js 文件,通过代码分割成单元片段并按需加载. 2.webpack的优势: ①.webpack 是以 commonJS 的形式来书写脚 ...
- 第二章 构建布局良好的windows程序
工具箱→菜单和工具栏 菜单栏 MenuStrip的类型 MenuItem:菜单项 TextBox:文本框 ComboBoX:组合框 Separato:分割线 前面都有ToolStrip做前缀 Appl ...
- 28-1-LTDC显示中英文
1.字符编码 由于计算机只能识别 0 和 1,文字也只能以 0 和 1 的形式在计算机里存储,所以我们需要对文字进行编码才能让计算机处理,编码的过程就是规定特定的 01 数字串表示特定的文字,最简单的 ...
- 第八节:分支开发之合并到master
流程:在客户端创建分支,修改代码,并push,然后在页面处理即可.(区别在于一个在客户端,一个在页面) 实际的开放中要记得打tag,不然到时候出问题了以后不知道从哪里开始.