题目来源:

  https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-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, inorder, postorder):
"""
:type inorder: List[int]
:type postorder: List[int]
:rtype: TreeNode
"""
def dfs(ibegin,iend,pbegin,pend):
if pbegin >= pend:
return None
if pbegin == pend - 1:
return TreeNode(postorder[pend - 1])
i = inorder.index(postorder[pend - 1]) - ibegin
ans = TreeNode(postorder[pend - 1])
ans.left = dfs(ibegin,ibegin+i,pbegin,pbegin + i)
ans.right = dfs(ibegin + 1 + i,iend,pbegin + i,pend - 1)
return ans
return dfs(0,len(inorder),0,len(postorder))

[LeetCode]题解(python):106-Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章

  1. 【LeetCode】105 & 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 ...

  2. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  3. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

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

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

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

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

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

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

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

随机推荐

  1. ZooKeeper源码阅读(二):客户端

    源代码: http://svn.apache.org/repos/asf/zookeeper/trunk/ 导入eclipse: 在包含build.xml目录下执行ant eclipse将产生.cla ...

  2. 漏掉的账目(用C语言去重)

    问题描述: 某财务部门结账时发现总金额不对头.很可能是从明细上漏掉了某1笔或几笔.如果已知明细账目清单,能通过编程找到漏掉的是哪1笔或几笔吗? 如果有多种可能,则输出所有可能的情况. 我们规定:用户输 ...

  3. 【Android进阶学习】shape和selector的结合使用

    shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector.可以这样说,shape和 ...

  4. 如何查看npm配置?

    npm config list //查看基本配置 npm config list -l //查看所有配置 npm本地安装与全局安装有什么区别? npm install grunt // 本地安装,则是 ...

  5. Android程序捕获未处理异常,处理与第三方方法冲突时的异常传递

    自己的android程序对异常进行了处理,用的也是网上比较流行的CrashHandler,代码如下,就是出现了未处理的异常程序退出,并收集收集设备信息和错误信息仪器保存到SD卡,这里没有上传到服务器. ...

  6. NET项目反编译+VS解决方案整理流程

    net项目反编译 工具:De4Dot + IL SPY和Reflector结合使用 项目:vs10+创建解决方案,每个类库尽量按照dll名来命名,方便整合,新建web项目先把aspx等文件拷贝进去,注 ...

  7. WCF---服务发布的步骤

    服务发布的步骤: 1.打开你的VS2012网站项目,右键点击项目>菜单中 重新生成一下网站项目:再次点击右键>发布: 2.弹出网站发布设置面板,点击<新建..>,创建新的发布配 ...

  8. 155Min Stack

    题目地址:155Min Stack 最近为了提高数据结构和算法能力,保证每天一到leetcode的题目.从easy开始,感觉这道题目还蛮好,记录一下. 题目大意:就是维护一个栈,获得栈中元素的的最小值 ...

  9. APM代码学习笔记3:执行过程

    以Linux平台ArduPlane为例 \ArduPlane\Plane.cpp 定义Plane类 继承自AP_HAL::HAL::Callbacks ,获取hal对象. \ArduPlane\Ard ...

  10. LINUX诞生

    编辑 Linux[1]  是一类Unix计算机操作系统的统称.Linux操作系统也是自由软件和开放源代码发展中最著名的例子.在1991 年的十月,由一个名为 Linus Torvalds的年轻芬兰大学 ...