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

标签: LeetCode


题目地址:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/

题目描述:

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]
Return the following binary tree: 3
/ \
9 20
/ \
15 7

题目大意

根据中序遍历和后序遍历重建二叉树。

解题方法

这个是套路题,我没有完全记住每一步是多少,而是根据树的样子和两个数组进行分析,得出切片的位置。

后序遍历的最后一个元素一定是根节点,在中序遍历中找出此根节点的位置序号。中序遍历序号左边的是左孩子,右边的是右孩子。再根据左孩子和右孩子的长度对后序遍历进行切片即可。

# 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
"""
if not inorder or not postorder: return None
val = postorder[-1]
root = TreeNode(val)
index = inorder.index(val)
root.left = self.buildTree(inorder[:index], postorder[:index])
root.right = self.buildTree(inorder[index+1:], postorder[index:-1])
return root

日期

2018 年 3 月 12 日

【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告的更多相关文章

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

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

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

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

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

  8. Leetcode#106 Construct Binary Tree from Inorder and Postorder Traversal

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  9. [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

    原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...

随机推荐

  1. html5的canvas鼠标点击画圆

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  2. echarts中饼状图数据太多进行翻页

    echarts饼状图数据太多 echarts 饼状图内容太多怎么处理 有些时候,我们饼状图中echarts的数据可能会很多. 这个时候展示肯定会密密麻麻的.导致显示很凌乱 我们需要'翻页'类似表格展示 ...

  3. act

    act的词源是do, 干着或干了的事情也可以叫act.抄全字典的话,抄的和看的都麻烦,在阅读中体会吧. act和action有啥区别?action: doing sth; act: n. action ...

  4. Spark基础:(七)Spark Streaming入门

    介绍 1.是spark core的扩展,针对实时数据流处理,具有可扩展.高吞吐量.容错. 数据可以是来自于kafka,flume,tcpsocket,使用高级函数(map reduce filter ...

  5. Oracle参数文件—pfile与spfile

    oracle的参数文件:pfile和spfile 1.pfile和spfile       Oracle中的参数文件是一个包含一系列参数以及参数对应值的操作系统文件.它们是在数据库实例启动时候加载的, ...

  6. 3.5 Rust Generic Types, Traits, and Lifetimes

    Every programming language has tools for effectively handling the duplication of concepts. In Rust, ...

  7. OpenStack之五: image镜像服务(端口9292)

    官网地址:https://docs.openstack.org/glance/stein/install/install-rdo.html #:创建glance库,并授权 MariaDB [(none ...

  8. Function Overloading in C++

    In C++, following function declarations cannot be overloaded. (1)Function declarations that differ o ...

  9. Google Guava 常用集合方法

    /** * Author: momo * Date: 2018/6/7 * Description: */ public class ListTest { public static void mai ...

  10. 如何使用redis作为缓存,增强用户访问数据的用户体验

    /**完成步骤 1.创建关系型数据库mysql的Provice库,同时启动nosql系列的redis数据库 2.创建项目,导入相关的jar包 3.创建jedis/utils/domain/dao/se ...