【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link:
https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
The basic idea is same to that for Construct Binary Tree from Inorder and Postorder Traversal. We solve it using a recursive function. First, we find preorder[0] in inorder, let's say inorder[i] == preorder, then construct the left tree from preorder[1..i] and inorder[0..i-1] and the right tree from preorder[i+1..n-1] and inorder[i+1..n-1].
The code is as follows.
# 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):
n = len(preorder)
if n == 0:
return None
elif n == 1:
return TreeNode(preorder[0])
else:
mid_inorder = inorder.index(preorder[0])
root = TreeNode(preorder[0])
root.left = self.buildTree(preorder[1:mid_inorder+1], inorder[:mid_inorder])
root.right = self.buildTree(preorder[mid_inorder+1:], inorder[mid_inorder+1:])
return root
【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- LeetCode OJ 105. 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 OJ: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】【Medium】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
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【树】Construct Binary Tree from Preorder and Inorder Traversal
题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后 ...
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★
1.题目 2.思路 3.java代码 //测试 public class BuildTreeUsingInorderAndPreorder { public static void main(Stri ...
- 【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 ...
随机推荐
- js 中使用el表达式 关键总结:在js中使用el表达式一定要使用双引号
js 中使用el表达式 关键总结:在js中使用el表达式一定要加双引号 js控制中用到了el表达式,最开始源码如下: var selected = ${requestScope.xxxxForm.re ...
- 自己不懂的SQL语句用法
left join:是SQL语言中的查询类型,即连接查询.它的全称为左外连接,是外连接的一种. 连接通常可以在select语句的from子句或where子句中建立,其语法格式为: select c ...
- PHP7 错误处理
最近学习了下PHP7新特性教程,记录了一些学习笔记. PHP 7 改变了大多数错误的报告方式.不同于 PHP 5 的传统错误报告机制,现在大多数错误被作为 Error 异常抛出. 这种 Error 异 ...
- Eclipse智能提示及快捷键
1.java智能提示 (1). 打开Eclipse,选择打开" Window - Preferences". (2). 在目录树上选择"Java-Editor-Conte ...
- nginx 安装与配置
centos7环境下nginx的安装 版本 0.85 tar zxvf nginx.tar.gz cd nginx ./configure // ./configure --help 查看编译选项 ...
- 理解js中__proto__和prototype的区别和关系
首先,要明确几个点:1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性__proto ...
- 在Ubuntu环境把PPT和Word转换为swf文件
项目需要一个在线浏览文档的功能,于是参照网上的代码写了一份利用Microsoft Office 2010和swftools-2013-04-09-1007.exe转换的程序 思路:调用电脑本机的off ...
- 【初级】linux mv 命令详解及使用方法实战
mv:移动文件或者将文件改名 前言: mv是move的缩写,顾名思义是移动.它的功能既能移动文件/文件夹,又可以用来改名,经常用来做文件的备份,比如再删除之前,先给文件做备份(保护数据)也是linux ...
- uboot mmc烧写命令
mmc write addr blk# cnt 这个命令的作用是将内存上的数据写入mmc中 参数: addr: 从内存读取的位置 blk: 写入到mmc中block位置,这个位置是mmc的0地址的偏移 ...
- liunx 下 部署并运行java项目(非web)
1. 将这三个包上传到liunx上,之后写一个run.sh 的脚本文件,然后在lib包中上传包<sunjce-provider.jar>包. 2.启动run.sh( ./run.sh st ...