题目来源:

  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. 利用KVO监视一个view的frame

    首先,keyPath一定是frame,而不是frame.origin.x之类的路径,因为再点下去的话,就是访问结构体内部的值了,KVO是无法检测的,会报错找不到KeyPath. 代码如下: [_fun ...

  2. winds引导配置数据文件包含的os项目无效

    我装了winds7与linux双系统,用easyBcd程序时,删除了一个winds7,之后winds7就进不去了, 进入winds7时显示winds未能启动,原因可能是最近更改了硬件或软件.解决此问题 ...

  3. 今天,Java编程周末提高班(第一期)正式结束

    Java编程周末提高班(第一期),走过了近两个月历程,一共同拥有68人次学生周末到老师家进行Java学习与交流.近距离的和一群年轻的学习接触,收获非常多,特别是对以后教学的改进.在学习的闲暇.大家自己 ...

  4. Spring MVC 简单介绍

    Spring MVC 是典型的mvc架构,适合web开发. controler 输入输出的控制器,也是对外view提供数据的接口,调用service层. model 数据,由bean组成(相应表),关 ...

  5. xcode xib 加载 、注意点

    加载xib2中方式 NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"xib名称" owner:nil options: ...

  6. Bootstrap的aria-label与aria-labelledby

    aria-label: 正常情况下,form表单的input组件都有对应的label,当input组件获取到焦点时,屏幕阅读器会读出相应label里的文本. 但是如果没有给输入框设置label时,当其 ...

  7. win7环境下安装MongoDB

    1.从http://www.mongodb.org/downloads获取,下载适合windows版本的mongodb,注意32位和64位的区别2.将下载的zip版本,解压到D:/mongodb3.创 ...

  8. Javascript/Jquery 中each() 和forEach()的区别

    从名字看上去这两个方法好像有点关系,但在javascript中它们区别还是挺大的. forEach() 用于数组的操作,对数组中的每个元素执行制定的函数(不是数组不能使用forEach()方法). 而 ...

  9. PHP学习遇到的问题

    使用php ob_start()模板生成html 内容无法撑开 生成后主要的内容 <body><warp><main> 生成后这个几个节点总是固定大小,不能被内容撑 ...

  10. perl正则表达式第三周笔记

    正则引擎的分类 正则引擎的分类 正则引擎的分类主要分两种: DFA:egrep.awk.lex.flex NFA:.NET.PHP.Perl.Ruby.Python.GNU Emacs.ed.sec. ...