原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/

题意:根据二叉树的中序遍历和后序遍历恢复二叉树。

解题思路:看到树首先想到要用递归来解题。以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序遍历为{4,5,2,6,7,3,1},我们可以反推回去。由于后序遍历的最后一个节点就是树的根。也就是root=1,然后我们在中序遍历中搜索1,可以看到中序遍历的第四个数是1,也就是root。根据中序遍历的定义,1左边的数{4,2,5}就是左子树的中序遍历,1右边的数{6,3,7}就是右子树的中序遍历。而对于后序遍历来讲,一定是先后序遍历完左子树,再后序遍历完右子树,最后遍历根。于是可以推出:{4,5,2}就是左子树的后序遍历,{6,3,7}就是右子树的后序遍历。而我们已经知道{4,2,5}就是左子树的中序遍历,{6,3,7}就是右子树的中序遍历。再进行递归就可以解决问题了。

代码:

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param inorder, a list of integers
# @param postorder, a list of integers
# @return a tree node
def buildTree(self, inorder, postorder):
if len(inorder) == 0:
return None
if len(inorder) == 1:
return TreeNode(inorder[0])
root = TreeNode(postorder[len(postorder) - 1])
index = inorder.index(postorder[len(postorder) - 1])
root.left = self.buildTree(inorder[ 0 : index ], postorder[ 0 : index ])
root.right = self.buildTree(inorder[ index + 1 : len(inorder) ], postorder[ index : len(postorder) - 1 ])
return root

[leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python的更多相关文章

  1. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  2. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

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

  3. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

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

  5. LeetCode——Construct Binary Tree from Inorder and Postorder Traversal

    Question Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may a ...

  6. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

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

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

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

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

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

随机推荐

  1. 安卓代码混淆(Android Studio)

    Proguard是安卓提供的方便开发者对代码和apk进行保护和精简的工具,可在SDK/tools文件夹下找到.   proguard的作用 : 1,代码混淆 2,精简代码,删掉没有用到的代码,减小ap ...

  2. Matlab 也很强大!

    一.实时编辑器 所创建的脚本不仅可以捕获代码,还可以讲述与人分享的故事.自动化的上下文提示可让您在编程时快速推进,并且将结果与可视化内容和您的代码一起显示. 一般以 .mlx 为后缀. 二.App D ...

  3. java I/O流类概述

    java I/O流类概述

  4. python获取当前系统的桌面的路径

    一,用内置的winreg(推荐) import winregdef get_desktop():    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,\  ...

  5. 解决mongo 端口占用问题

    在打开mongod之后如果不用了就按ctrl + c ,就不会出现以下的问题了 执行mongod报错 mongod 2016-08-03T14:31:15.691+0800 I CONTROL [in ...

  6. python列表中中文编码的问题

    在python2列表中,有时候,想打印一个列表,会出现如下显示: 这个是由于: print一个对象,是输出其“为了给人(最终用户)阅读”而设计的输出形式,那么字符串中的转义字符需要转出来,而且 也不要 ...

  7. spring---aop(5)---Spring AOP的配置的背后的配置

    写在前面 Spring AOP中Pointcut,dvice 和 Advisor三个概念 1)切入点 Pointcut 在介绍Pointcut之前,有必要先介绍 Join Point(连接点)概念. ...

  8. MacBook pro new 触控板手势及快捷键

    MacBook pro new 触控板手势:   显示桌面: 拇指+三指 向外张开 launchpad: 拇指+三指 向中间集中   正在运行的窗口:三指向上 应用浏览: 三指向下 窗口信息: 三指点 ...

  9. tomcat+java的web程序持续占cpu问题调试

    原文出处:http://www.blogjava.net/hankchen 现象: 在tomcat中部署java的web应用程序,过一段时间后出现tomcat的java进程持续占用cpu高达100%, ...

  10. CentOS的update-grub2命令

    这个和Ubuntu还是有些区别,在CentOS修改成如下: grub2-mkconfig -o /boot/grub2/grub.cfg