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的更多相关文章

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

  2. (二叉树 递归) 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 ...

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

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

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

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

  7. [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

    原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...

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

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

随机推荐

  1. 洛谷P1605 迷宫【dfs】

    题目背景 迷宫 [问题描述] 给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过.给定起点坐标和 终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案.在迷宫 中移动有上下 ...

  2. C# 方法中的this参数

    x 先看下面的代码: public static class StringExtension { public static void Foo(this string s) { Console.Wri ...

  3. css学习_css盒模型及应用

    1.看透网页布局的本质 2.盒模型 margin.border.padding.width.height a. border: 1px solid red                (solid/ ...

  4. 品尝阿里云容器服务:5个2核4G节点使用情况记载

    使用5台2核4G非IO优化的ECS作为节点创建集群,节点操作系统是Ubuntu 16.04.2 LTS.创建后3个为mananger节点,2个为worker节点,每个节点默认会运行7个容器,其中3个s ...

  5. MySQL的JDBC驱动源码解析

    原文:   MySQL的JDBC驱动源码解析 大家都知道JDBC是Java访问数据库的一套规范,具体访问数据库的细节有各个数据库厂商自己实现 Java数据库连接(JDBC)由一组用 Java 编程语言 ...

  6. [No0000F9]C# 运算符重载

    您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的.与其他函数一样,重载运 ...

  7. 不可访问内存 Java四种引用包括强引用,软引用,弱引用,虚引用

    小结: 1.不可访问内存是指一组没有任何可访问指针指向的由计算机程序进行动态分配的内存块. 2.垃圾收集器能决定是否一个对象还是可访问的:任何被确定不可访问的对象将会被释放. https://zh.w ...

  8. WinAPI Hook

    1.抢先load 需要hook的dll,替换需要hook的函数的地址, 2.调用堆栈信息的获取: 3.内存信息的统计: 4.如何统计已经free掉的内存? 5.如何批量注入程序load的dll? IA ...

  9. 虚拟机窗口太小_设置分辨率(win8/win7)

    虚拟机安装了WIN7和WIN8系统后,安装了VMware Tools后窗口还是较小,需要调整虚拟机系统中的分辨率. 桌面右键->屏幕分辨率->设置成与主机显示器分辨率相近的即可.

  10. linux关闭的时候出现异常: java.net.ConnectException: 拒绝连接 (Connection refused)

    这个时候: 需要先把java进程杀死. 然后再重新启动. 杀死进程: ps -aux | grep java 然后kill -9 进程 然后再重新启动.