【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 ...
随机推荐
- win7远程连接 您的凭据不工作
1.查看远程连接有没有打开:计算机->属性->远程设置. 2.如果上面开启了还是不行,找到开始--- 运行-- 输入 gpedit.msc,打开注册表编辑器:然后依次找到菜单,计算机配置- ...
- T4模板批量生成代码
大家通过比对下,就应该知道怎么玩. T4代码 <#@ template debug="false" hostspecific="true" languag ...
- python【4】-函数
一. 定义函数 定义一个函数要使用def语句,依次写出函数名.括号.括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回.如果没有return语句,函数执行完毕后也会返 ...
- Android在代码中使用布局文件中的一个组件
使用前必须要把组件与其父组件的关系断开,比如有一个组件的名称为scrollChildLayout,则可以使用下面的代码进行分离 ((ViewGroup)scrollChildLayout.getPar ...
- android .9图的作用
参考:http://www.cnblogs.com/lianghui66/archive/2013/01/08/2850581.html .9图的介绍 1.这种格式的图片是Android平台上一种被拉 ...
- C# in VS
1. DllImport是System.Runtime.InteropServices命名空间下与与非托管相关的一个属性类,负责导出从非托管的dll中导出函数信息,导出的函数在声明时必须有extern ...
- [转]MYSQL高可用方案探究(总结)
前言 http://blog.chinaunix.net/uid-20639775-id-3337432.htmlLvs+Keepalived+Mysql单点写入主主同步高可用方案 http://bl ...
- [转]CSS3 Media Query实现响应布局
讲到响应式布局, 相信大家都有一定的了解,响应式布局是今年很流行的一个设计理念,随着移动互联网的盛行,为解决如今各式各样的浏览器分辨率以及不同移动设备的显示效果, 设计师提出了响应式布局的设计方案.今 ...
- adapter用法
Android之Adapter用法总结 1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView) ...
- WPS for Linux(ubuntu)字体配置(字体缺失解决办法)
启动WPS for Linux后,出现提示"系统缺失字体" . 出现提示的原因是因为WPS for Linux没有自带windows的字体,只要在Linux系统中加载字体即可. 具 ...