题目来源:

  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的更多相关文章

  1. 【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 ...

  2. [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 ...

  3. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  4. 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 ...

  5. 【一天一道LeetCode】#105. Construct Binary Tree from Preorder and Inorder Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

  6. (二叉树 递归) 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. 【每日一摩斯】-Troubleshooting: High CPU Utilization (164768.1) - 系列6

    如果问题是一个正运行的缓慢的查询SQL,那么就应该对该查询进行调优,避免它耗费过高的CPU资源.如果它做了许多的hash连接和全表扫描,那么就应该添加索引以提高效率. 下面的文章可以帮助判断查询的问题 ...

  2. cocos2d-x 3.0rc2 对于每个包执行情况的重要平台 (超级方便)

    首先,你需要下载三个文件:每间 android-ndk android-sdk ant 下载位置可以随意:由于3.0rc2执行setup.py  自己主动搜索这三个文件 win32cmd以下: (1) ...

  3. Android 教你打造炫酷的ViewPagerIndicator 不仅仅是高仿MIUI

    1.概述 哈,今天给大家带来一个ViewPagerIndicator的制作,相信大家在做tabIndicator的时候,大多数人都用过 TabPageIndicator,并且很多知名APP都使用过这个 ...

  4. jquery validate.addMethod 正则表达式 (自定义验证方法)

    项目中使用的jQuery添加的校验的方法 $(document).ready(function(){         5           6/* 设置默认属性 */         7$.vali ...

  5. Java中使用Observer接口和Observable类实践Observer观察者模式

    在Java中通过Observable类和Observer接口实现了观察者模式.实现Observer接口的对象是观察者,继承Observable的对象是被观察者. 1. 实现观察者模式 实现观察者模式非 ...

  6. Sql"列转行"三种方法对比

    SQL code------ 合并列值  --***************************************************************************** ...

  7. 几个SQL

    select sum(`value`) from testtable where value != 'error' AND type ='b' in (select DISTINCT(type) fr ...

  8. SSH框架的简化

    ---恢复内容开始--- 一.简易化的SSH框架,如图: SSH框架的搭建,我就不多说了. 二.简易的ssh框架的步骤: 1.重新编写applicationContext.xmlwen文件 <一 ...

  9. 对常量的引用(reference to const)的一般用途(转载)

    如果是对一个常量进行引用,则编译器首先建立一个临时变量,然后将该常量的值置入临时变量中,对该引用的操作就是对该临时变量的操作.对C++常量引用可以用其它任何引用来初始化:但不能改变. 关于引用的初始化 ...

  10. Windows主机和Linux虚拟机之间传输文件

    如果使用VirtualBox的增强功能, 可以实现两者之间文件相互拖拽. 但某些情况下, 比如增强功能安装遇到难以解决的问题, 或者Linux版本为server版本(例如Ubuntu Server发行 ...