【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 ...
随机推荐
- 编译android源码官方教程(6)编译内核
Building Kernels IN THIS DOCUMENT Selecting a kernel Identifying kernel version Downloading sources ...
- jQuery选择器的优化选择
jQuery选择器的优化选择 1.1 属性选择器 var $div=$("[id]"); 选中拥有该属性的元素 var $div=$("[id=div]"); ...
- aws在线技术峰会笔记-游戏解决方案
选项1:可以将aws的SDK嵌入到APP中. 选项2:Mobile Hub自动生成代码. 选项3:开源免费的游戏引擎.可视化脚本编程,实现客户端的逻辑代码. 用户管理 Cognito Identity ...
- c#中对rgb的使用
今天发现c#中没有和vb类似的函数RGB(), 后来发现可以通过Color类来实现 从R,G,B的值可以得到一个Color,如: Color c = Color.FromArgb(255,0,0); ...
- TCP/IP协议学习(四) 协议概述
生活中有舒适区,借口成为懒惰的护身符,学习也有舒适区,逃避便是阻止进步的最大障碍. 经过半年多嵌入式方面的工作和学习,我提高了很多,但同时我也对自己所面临的问题逐渐清晰: 1. 偏于实践,理论基础不牢 ...
- webstrom使用方法
一.设置file-settings- -color&fonts设置,字体 主体 -file and code templates模板ctrl+r 查找,替换1 双击shift 快速查找2 fi ...
- 做个这样的APP要多久?[转]
这是一个“如有雷同,纯属巧合”的故事,外加一些废话,大家请勿对号入座.开始了…… 我有些尴尬地拿着水杯,正对面坐着来访的王总,他是在别处打拼的人,这几年据说收获颇丰,见移动互联网如火如荼,自然也想着要 ...
- 转!!XML,DTD,XSD,XSL的区别
XML=可扩展标记语言(eXtensible Markup Language).可扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可用 方便的方式建立,虽然XML占 ...
- html中button自动提交表单?
在ie中,button默认的type是button,而其他浏览器和W3C标准中button默认的属性都是submit
- 图的深度优先和广度优先遍历(图以邻接表表示,由C++面向对象实现)
学习了图的深度优先和广度优先遍历,发现不管是教材还是网上,大都为C语言函数式实现,为了加深理解,我以C++面向对象的方式把图的深度优先和广度优先遍历重写了一遍. 废话不多说,直接上代码: #inclu ...