【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
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的更多相关文章
- 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 ...
- 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 ...
- 【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 ...
- 【题解二连发】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 ...
- 【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 ...
- 【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 ...
- 【树】Construct Binary Tree from Inorder and Postorder Traversal
题目: Given inorder and postorder traversal of a tree, construct the binary tree. 思路: 后序序列的最后一个元素就是树根, ...
- 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 ...
- 【LeetCode106】Construct Binary Tree from Inorder and Postorder Traversal★★
1.题目 2.思路 思路和LeetCode105类似,见上篇. 3.java代码 //测试 public class BuildTreeUsingInorderAndPostorder { publi ...
随机推荐
- 第二章:Posix IPC
2.1:概述 以下三种类型的IPC合称为“Posix IPC”: Posix消息队列 Posix信号量 Posix共享内存区 Posix IPC在访问它们的函数和描述它们的信息上有一些类似点.本章讲述 ...
- Jeff Dean
"--出自"关于 Jeff Dean 的事实" 其实,"关于 Jeff Dean 的事实"这个G+ 帖中描述的并非是真实的.不过有人大费周折为他建立了 ...
- <转>linux crontab 定时任务
基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟1-59 每分钟用*或者 */1表示 第2列表示小时1-23(0表示0点) 第3列表示日期1-31 ...
- 大分享-hibernate,springmvc,easyui简要介绍
近期公司一直在做项目,主要用到了springMVC,eseayui,hibernate几大框架.近一个月的时间,个人就目前自我知识给予分享. 很多公司使用mybatis产品,综合所述其最大优点是全SQ ...
- C++实现树的基本操作,界面友好,操作方便,运行流畅,运用模板
Ⅰ.说明: .采用左孩子右兄弟的方式,转化为二叉树来实现. .树的后根遍历与二叉树的中根遍历即有联系又有区别,请读者注意分析体会. Ⅱ.功能: .创建树并写入数据 .先根遍历树 .计算树高 .后根遍历 ...
- hihoCoder 1383 : The Book List(书目表)
hihoCoder #1383 : The Book List(书目表) 时间限制:1000ms 单点时限:1000ms 内存限制:256MB Description - 题目描述 The histo ...
- Java处理excel文件
好久好久没写blog了,感觉都生锈了,最近弄了弄java处理excel,特来简单粘贴一下: package excel; import java.io.BufferedInputStream; imp ...
- 基础笔记4(包装类,时间date. calendar
1.包装类 基本类型和对象. 编译器会对基本类型和包装类进行自动拆箱,装箱处理 Interger i=5; int i=new Interger(4); 一个缓存问题:以便提高效率 integer ...
- asp获取虚拟目录根路径
<% TempArray = Array("account_book","admin","ajaxFile","catalo ...
- C# 字符串转义和反转义
System.Text.RegularExpressions.Regex.Unescape(s) System.Text.RegularExpressions.Regex.Escape(s)