题目来源:

  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. Crisis of HDU(母函数)

    Crisis of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. 音乐ID3 中 专辑封面解析(APIC帧)

    ID3V2 中 APIC 帧标识 专辑封面.前几天 百度 谷歌 都没有找到具体的说明.有点小伤人. 最好参考  Android 中的 id3.cpp 以及一个java 开源 id3 库.找到这里的规格 ...

  3. css重置

    清除标签的默认样式 body{margin:0;}ul,ol{margin:0 auto;padding:0;}li{ list-style:none;}dl{margin:0;}dd{margin: ...

  4. iis 配置php

    1.CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一种语 ...

  5. hive 学习笔记精简

    创建表: drop table t create table if not exists t (t string) partitioned by (log_date string) row forma ...

  6. HelloWorld——Cocos2d-x学习历程(二)

    HelloWorld分析: 1."resource"文件夹 该文件夹主要用于存放游戏中需要的图片.音频和配置等资源文件. 2."include"和"s ...

  7. D - Common Subsequence

    D - Common Subsequence Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I ...

  8. Android学习笔记(不定时更新)

    <2014-03-20>设置按钮的不同状态 1.res/ layout/ [文件名]myselector.xml 2.把两张不同的按钮图片放到drawable-xxxx文件夹里,mysel ...

  9. MYSQL group_concat() 函数

    看来看一下表中的数据 select * from t; 下一步来看一下group_concat函数的用法 select ID,group_concat(Name) from t group by ID ...

  10. label 和 legend标签的用法

    label 和 legend标签的用法 label标准用法: 一般浏览器都支持 一般而言,label标签位于表单元素的前面或者后面,为控件提供说明文字 <label for="user ...