[leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
题意:根据二叉树的先序遍历和中序遍历恢复二叉树。
解题思路:可以参照 http://www.cnblogs.com/zuoyuan/p/3720138.html 的思路。递归进行解决。
代码:
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param preorder, a list of integers
# @param inorder, a list of integers
# @return a tree node
def buildTree(self, preorder, inorder):
if len(preorder) == 0:
return None
if len(preorder) == 1:
return TreeNode(preorder[0])
root = TreeNode(preorder[0])
index = inorder.index(root.val)
root.left = self.buildTree(preorder[1 : index + 1], inorder[0 : index])
root.right = self.buildTree(preorder[index + 1 : len(preorder)], inorder[index + 1 : len(inorder)])
return root
[leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python的更多相关文章
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Leetcode Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode——Construct Binary Tree from Preorder and Inorder Traversal
Question Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as ...
- Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal
总结: 1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标 代码: 前序中序 #include <iostream> #include <vector> usi ...
- 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 ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- 【题解二连发】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 - LeetCode Construct Binary ...
随机推荐
- C++雾中风景5:Explicit's better than implicit.聊聊Explicit.
关于Explicit还是Implicit一直是编程语言中能让程序员们干起架的争议.那些聪明的老鸟总是觉得Implicit的规则让他们能够一目十行,减少样板代码的羁绊.而很多时候,Implicit的很多 ...
- CSUOJ 1007 矩形着色
Description Danni想为屏幕上的一个矩形着色,但是她想到了一个问题.当点击鼠标以后电脑是如何判断填充的区域呢? 现在给你一个平面直角坐标系,其中有一个矩形和一个点,矩形的四条边均是平行于 ...
- Kolla O版本部署
Kolla O版部署和之前的版本还是有些区别的,环境还是all-in-one 基本准备: 关闭Selina和firewalld [root@kolla ~]# cat /etc/redhat-rele ...
- 防止sql注入的几种方法
一.SQL注入简介 SQL注入是比较常见的网络攻击方式之一,它不是利用操作系统的BUG来实现攻击,而是针对程序员编程时的疏忽,通过SQL语句,实现无帐号登录,甚至篡改数据库. 二.SQL注入攻击的总体 ...
- Centos 安装 WPS
Linux有自己的一套类是Office的办公软件:LibreOffice,但是不是很友好,幸好WPS有Linux版本. 安装步骤: 1.安装依赖包 yum install libpng12 yum i ...
- 2018/3/20 noip模拟赛 5分
T1 傻逼题,写了cmp没sort,5分. T2 树上差分,写了树剖线段树,超时,0分. T3 树归,0分. 我是个zz
- 本地文件包含漏洞(LFI漏洞)
0x00 前言 本文的主要目的是分享在服务器遭受文件包含漏洞时,使用各种技术对Web服务器进行攻击的想法. 我们都知道LFI漏洞允许用户通过在URL中包括一个文件.在本文中,我使用了bWAPP和DVW ...
- VMWare虚拟机下CentOS 配置网络实现远程连接,提供Web访问
最近使用VMWARE虚拟机当中redhat操作系统,感觉直接使用很不方便,于是就决定配置下redhat网络,通过本机远程工具SecureCRT来连接redhat使用. 环境说明:本机操 ...
- bzoj 3275 最小割
给你一堆东西,叫你选一些东西出来,使得价值最大,要求选出的东西集合中的任意a,b满足性质p. 可以考虑: 1.拟阵? 2.二分图? 这道题由于数学硬伤,不知道不存在两条直角边是奇数,斜边是整数的直角三 ...
- Windows下ftp服务器搭建及配置
Win系统使用ser-u软件进行FTP服务器的搭建下载地址:https://www.serv-u.com/操作步骤如下:1. 点击执行程序进行按照SU-FTP-Server-Windows-v15.1 ...