[LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal
题目来源:
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
题意分析:
根据二叉树的中序和前序遍历结果,反推这个树,假设只有一个这样的树。
题目思路:
前序遍历的第一个树将中序遍历分成两半,左半部分是左子树,右半部分是右子树,递归重构这棵树。
代码(python):
# 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, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
def dfs(pbegin,pend,ibegin,iend):
if pbegin >= pend:
return None
if pbegin + 1 == pend:
return TreeNode(preorder[pbegin])
i = inorder.index(preorder[pbegin])
i -= ibegin
ans = TreeNode(preorder[pbegin])
ans.left = dfs(pbegin + 1,pbegin + i + 1,ibegin,ibegin+i)
ans.right = dfs(pbegin + i + 1,pend,ibegin + 1 + i,iend)
return ans
return dfs(0,len(preorder),0,len(inorder))
[LeetCode]题解(python):105-Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Java for LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...
随机推荐
- IOS开发:xcode5版本引发的问题
下面这段代码是用于处理ios7头部透明问题的 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000 if ( IOS7_OR_LATER ) { self.e ...
- 投资学第一章 investments-introduction
转载请注明来自souldak,微博:@evagle 砖搬多了有点累,今天学学投资学. 这章主要是一些重要的概念. real assets / financial assets , fixed-in ...
- English - according to 的用法说明
1. 用于according to,意为“根据”,为复合介词,后接名词或代词.注意以下用法: (1) 主要用来表示“根据”某学说.某书刊.某文件.某人所说等或表示“按照”某法律.某规定.某惯例.某情况 ...
- JDK,JRE,JVM区别与联系(转)
JDK : JavaDevelopment ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工具 ...
- Problem G: If We Were a Child Again
Problem G: If We Were a Child AgainTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solved: 14[Submi ...
- leetcode find median sorted arrays python
# @link http://www.cnblogs.com/zuoyuan/p/3759682.html class Solution(object): def findMedianSortedAr ...
- Spring管理Hibernate
为什么要用Hibernate框架? 既然用Hibernate框架访问管理持久层,那为何又提到用Spring来管理以及整合Hibernate呢? 首先我们来看一下Hibernate进行操作的步骤.比如添 ...
- python 学习之Windows 下的编码处理!
问题1: Non-ASCII character '\xe9' in file 问题原因:程序编码上出现问题 解决方法:在程序头部加上代码 #-*- coding: UTF-8 -*- 设置代码编码为 ...
- while 、do...while 、for
1.while 特点:只有条件成立才会执行循环体. while陷阱: while(条件);即直接加分号 2.do while 特点:一定会执行一次循环体 3.for语句 l 初始化等可以是多句(把 ...
- 测试scanf输入含非法控制符
心得: 学到scanf命令时第一个想到的就是可以利用scanf做一个十进制转16进制.八进制的小程序,很天真的以为也可以转二进制,在搜索字符控制符的时候才知道原来没有二进制的控制字符,需要换算出来得出 ...