Problem Link:

https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/

This problem can be easily solved using recursive method.

By given the inorder and postorder lists of the tree, i.e. inorder[1..n] and postorder[1..n], so postorder[n] should be the root's value. Then, we find the position of postorder[n] in inorder[1..n], suppose the position is i, then postorder[1..i-1] and inorder[1..i-1] are the postorder and inorder lists of root's left tree and postorder[i..n-1] and inorder[i+1..n] are the postorder and inorder lists of root's right tree. So we can construct the tree recursively.

The code of the recursive function is as follows.

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param inorder, a list of integers
# @param postorder, a list of integers
# @return a tree node
def buildTree(self, inorder, postorder):
n = len(inorder)
if n == 0:
return None
elif n == 1:
return TreeNode(postorder[-1])
else:
root = TreeNode(postorder[-1])
mid_inorder = inorder.index(postorder[-1])
root.left = self.buildTree(inorder[:mid_inorder], postorder[:mid_inorder])
root.right = self.buildTree(inorder[mid_inorder+1:], postorder[mid_inorder:-1])
return root

【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. LeetCode OJ 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 ...

  2. LeetCode OJ: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 OJ】Construct Binary Tree from Preorder and Inorder Traversal

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...

  4. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  5. 【Leetcode】【Medium】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 ...

  6. 【leetcode】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 ...

  7. 【树】Construct Binary Tree from Inorder and Postorder Traversal

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. 思路: 后序序列的最后一个元素就是树根, ...

  8. leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  9. 【LeetCode106】Construct Binary Tree from Inorder and Postorder Traversal★★

    1.题目 2.思路 思路和LeetCode105类似,见上篇. 3.java代码 //测试 public class BuildTreeUsingInorderAndPostorder { publi ...

随机推荐

  1. C#获取文件时间

    在NTFS下,文件的创建及修改时间可以精确到毫秒,以下是测试过程. DirectoryInfo diSource = new DirectoryInfo(@"C:\Users\不告诉你\De ...

  2. QT笔记之不规则窗口的实现

    QT实现的不规则窗口,是根据图片的形状显示 1.去标题栏 2.设置窗口背景为透明色 3.最后给窗口设置背景色 注:背景图为镂空的 格式为.png 图片资源下载:http://pan.baidu.com ...

  3. 使用phantomjs操作DOM并对页面进行截图需要注意的几个问题

    phantomjs是一个无界面浏览器,可用于网页截图和前端自动化测试,基于webkit内核(也就是chrome使用的内核),并使用js编写业务脚本来请求.浏览和操作页面.最近前端监控需要用到phant ...

  4. My97日期控件 选择日期区间

    <script language="javascript" type="text/javascript" src="My97DatePicker ...

  5. 《BI那点儿事》数据流转换——排序

    排序转换允许对数据流中的数据按照某一列进行排序.这是五个常用的转换之一.连接数据源打开编辑界面,编辑这种任务.不想设置为排序列的字段不要选中,默认情况下所有列都会选中.如图所示,按照TotalSuga ...

  6. sbrk与brk的使用小例子

    sbrk() 和 brk() - Unix的系统函数   sbrk()和brk() 系统的底层会维护一个位置,通过位置的移动完成内存的分配和回收.映射内存时 以一个内存页作为基本单位.   void* ...

  7. Python生成字体

      Python version 2.7 required, which was not found in the registry 参考:http://www.cnblogs.com/min0208 ...

  8. 程序设计入门——C语言 第2周编程练习 信号报告(5分)

      2   题目内容: 无线电台的RS制信号报告是由三两个部分组成的: R(Readability) 信号可辨度即清晰度. S(Strength)    信号强度即大小. 其中R位于报告第一位,共分5 ...

  9. oracle sql 优化

    2. 选择最有效率的表名顺序(只在基于规则的优化器中有效) ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,因此FROM子句中写在最后的表(基础表 driving table)将被最先 ...

  10. spring+mongo

    一.程序结构