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. noi 8787 数的划分

    题目链接:http://noi.openjudge.cn/ch0206/8787/ 将整数n分成k份,且每份不能为空,任意两份不能相同(不考虑顺序). 简直跟分苹果一模一样. #include < ...

  2. python除法

    1. >>> from __future__ import division >>> 2/3 0.6666666666666666 操作数为何种数值类型将影响结果 ...

  3. shell & dialog

    最近使用dialog写图形自动化shell脚本,  功能很强大,功能不是非常多但是足够用.想写一篇linux下dialog的使用方法,虽然命令不多,但是写起来也需要下很大功夫,而且不一定写得更好,在网 ...

  4. struct内存对齐1:gcc与VC的差别

    struct内存对齐:gcc与VC的差别 内存对齐是编译器为了便于CPU快速访问而采用的一项技术,对于不同的编译器有不同的处理方法. Win32平台下的微软VC编译器在默认情况下采用如下的对齐规则:  ...

  5. [问题2014S10] 复旦高等代数II(13级)每周一题(第十教学周)

    [问题2014S10]  设 \(A,B\) 为 \(n\) 阶方阵, 证明: \(AB\) 与 \(BA\) 相似的充分必要条件是 \[\mathrm{rank}\big((AB)^i\big)=\ ...

  6. oracle mysql sqlserver数据库中的分页

    oracle: select * from (select rownum r,t1.* from tablename t1 where rownum <M+N ) t2 where t2.r&g ...

  7. UITableView优化的方法

    使用不透明视图. 不透明的视图可以极大地提高渲染的速度.因此如非必要,可以将table cell及其子视图的opaque属性设为YES(默认值). 其中的特例包括背景色,它的alpha值应该为1(例如 ...

  8. CSS权重及样式优先级问题

    CSS权重值计算 一条样式规则的整体权重值包含四个独立的部分:[A, B, C, D]; (1) A 表示内联样式(写在标签的style属性中),只有 1 或者 0 两个值:对于内联样式,由于没有选择 ...

  9. oracle中如何创建dblink

    当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本地数据库一样访问远程数据库表中的数据.下面讲介绍如何在本地数 ...

  10. ASP.NET MVC 4 RC的JS/CSS打包压缩功能 Scripts.Render和Styles.Render

    打包(Bundling)及压缩(Minification)指的是将多个js文件或css文件打包成单一文件并压缩的做法,如此可减少浏览器需下载多个文件案才能完成网页显示的延迟感,同时通过移除JS/CSS ...