[LeetCode]题解(python):106-Construct Binary Tree from Inorder and Postorder Traversal
题目来源:
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的更多相关文章
- 【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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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: ...
- [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 ...
- 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 ...
- (二叉树 递归) 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- struts漏洞修补过程之S2-016
Struts漏洞修补过程之S2-016.邪恶的Struts再次现身,这一次是远程执行漏洞.官方建议立即升级到2.3.15.1.真希望这是最后一次漏洞修补.下面是升级步骤. 1.升级到struts2.3 ...
- c/c++中与字符串处理相关的函数
void *memccpy (void *dest, const void *src, int c, size_t n); 从src所指向的对象复制n个字符到dest所指向的对象中.如果复制过程中遇到 ...
- 7. Reverse Integer
1. 问题描述 Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321 cl ...
- 表A中有两个表示时间的字段A,B;如果B的值大于A的值,则把B的值更新为A的值
sql语句:表A中有两个表示时间的字段A,B:如果B的值大于A的值,则把B的值更新为A的值 update 表名 set B=A where B>A
- dedecms导入编辑器
<?php GetEditor("info","",450,"Diy"); ?>
- 如何调试框架中的app
1,在编写的app中添加断点,并重新生成或编译 2,找到框架app的相应位置代开文件把所用到的dll重新替换成上步生成的dll(bin->debug) 3,运行框架,在VS打开调试->附加 ...
- (4)事件处理——(2)在页面加载的时候执行任务(Performing tasks on page load)
We have already seen how to make jQuery react to the loading of a web page. The $(document).ready()e ...
- if最简单的用法
/* Name:if最简单的用法-1 Copyright:By.不懂网络 Author: Yangbin Date:2014年2月9日 03:00:58 Description:if最简单的用法,真则 ...
- SMTP邮件传输协议发送邮件和附件
在以前接触的项目中,一直都是在做网站时用到了发送mail 的功能,在asp 和.net 中都有相关的发送mail 的类, 实现起来非常简单.最近这段时间因工作需要在C++ 中使用发送mail 的功能, ...
- Eclipse中搭建Python开发环境
转载:http://www.cnblogs.com/realh/archive/2010/10/04/1841907.html Eclipse+PyDev环境搭建 1.准备工作 JDK6 Java 开 ...