【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python)
标签: LeetCode
题目描述:
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
题目大意
根据中序遍历和后序遍历重建二叉树。
解题方法
这个是套路题,我没有完全记住每一步是多少,而是根据树的样子和两个数组进行分析,得出切片的位置。
后序遍历的最后一个元素一定是根节点,在中序遍历中找出此根节点的位置序号。中序遍历序号左边的是左孩子,右边的是右孩子。再根据左孩子和右孩子的长度对后序遍历进行切片即可。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def buildTree(self, inorder, postorder):
"""
:type inorder: List[int]
:type postorder: List[int]
:rtype: TreeNode
"""
if not inorder or not postorder: return None
val = postorder[-1]
root = TreeNode(val)
index = inorder.index(val)
root.left = self.buildTree(inorder[:index], postorder[:index])
root.right = self.buildTree(inorder[index+1:], postorder[index:-1])
return root
日期
2018 年 3 月 12 日
【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告的更多相关文章
- 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: ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- (二叉树 递归) 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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal
原题地址 二叉树基本操作 [ ]O[ ] [ ][ ]O 代码: TreeNode *restore(vector<i ...
- [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)
原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...
随机推荐
- JAVA中null,"",equals,==相互之间使用详解
"equals" 与 "==" "equals"只是比较值是否相同 而"=="则是比较两个变量是不是同一个变量,也应时是 ...
- 搭建简单的SpringCloud项目二:服务层和消费层
GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 后篇:搭建简单的SpringCloud项目三 ...
- Hadoop入门 完全分布式运行模式-准备
目录 Hadoop运行环境 完全分布式运行模式(重点) scp secure copy 安全拷贝 1 hadoop102上的JDK文件推给103 2 hadoop103从102上拉取Hadoop文件 ...
- Django url中可以使用类视图.as_view()进行映射的原因
说明:在练习天天生鲜项目时,对利用类视图去与正则匹配到的url做映射有点疑惑,经过查看他人博客以及自我分析算是整明白了,所以记录一下 参考:https://www.zmrenwu.com/post/5 ...
- 单链表的模板类(C++)
/*header.h*/#pragma once #include<iostream> using namespace std; template<class T> struc ...
- Android权限级别(protectionLevel)
通常情况下,对于需要付费的操作以及可能涉及到用户隐私的操作,我们都会格外敏感. 出于上述考虑以及更多的安全考虑,Android中对一些访问进行了限制,如网络访问(需付费)以及获取联系人(涉及隐私)等. ...
- 字符串属性转变List属性存入数据库
项目中有系统IP字段,现将string转List存入数据库,每个功能块持久层实现方法不一样(分为jpa和mp) jpa: @Convert(converter = JpaConverterListJs ...
- spring boot项目创建与使用
概述 spring boot通常使用maven创建,重点在于pom.xml配置,有了pom.xml配置,可以先创建一个空的maven项目,然后从maven下载相关jar包. spring boot d ...
- my38_MySQL事务知识点零记
从innodb中查看事务信息 show engine innodb status\G; ------------ TRANSACTIONS------------Trx id counter 3153 ...
- 【Linux】【Problems】在fedora 9上解决依赖问题
summary: 在32bit的fedora9上安装EMC客户端遇到无法解决的依赖问题 detail: rpm 安装: [root@hcszmons02 tmp]# rpm -ivh lgtoclnt ...