【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 ...
随机推荐
- 拆分SharePoint 2013 中CreatedModifiedInfo 的时间
最近在自定义DisForm.aspx页面时 发现 创建时间信息无法进行拆分,人事MM只想要修改时间,去掉创建人,创建时间和修改人. 于是我的重新研究下在SPD里面如何去拆分这个时间. 在详情页面上找到 ...
- Python实践所遇问题记录
1.在cmd中直接输入'python'提示:'python'不是内部或外部命令,也不是可运行的程序或批处理文件. 原因:没有为Python设置环境变量. 解法:控制面板->系统->高级系统 ...
- 【思路】-分页-双top分页算法的原理
描述:实现分页的一种算法 大致过程:访客访问不同的分页,为这个当前页生成动态的查询SQL,然后送到数据库中执行 输入:总条数,每页多少条,第几页,查询的SQL,排序的字段 注意:传入的排序字段需要构成 ...
- PHP实现微信公众平台开发 全套视频资源下载
好久没有在博客园更新东西了,今天给大家分享一份比较不错的视频学习资源吧. 主要是关于PHP实现微信公众平台开发, 不知道大家对于微信平台的开发有多少了解,那么今天就从基础开始吧,资源目录如下(PS ...
- javasript_数据结构和算法_栈
//-----------------------------------存储结构为数组-------------------------------------------- function St ...
- Excel表格导入数据
步骤: 1,选择要插入的数据库--右键--任务--导入数据 2,点击下一步,选择数据源,excel文件路径,和版本信息(注:使用2010及以上版本的office,请先将格式转换为03 或07格式的以便 ...
- 用java实现简易PC版2048
import java.awt.Color; import java.awt.EventQueue; import java.awt.BorderLayout; import java.awt.Flo ...
- Spring(4)
Spring的Bean的配置形式 1.基于XML的形式(无需讲解) 2.基于注解的形式(需要引入AOP的jar包,此jar包实现了AOP的注解) 当在Spring配置文件中引入类扫描注解命名空间并且指 ...
- HDU 1405
题意: 输入一个数n,输出它的素因子与这个素因子出现的次数. 分析: 用欧拉函数,变下形就好了,不再过多解释. 代码如下: #include <iostream> #include < ...
- HOJ 1001: A+B; 1002: A+B+C
两道水题,用来熟悉 HOJ 的提交系统. 1001:输入两个整数 A, B (0 <= A,B <= 10),输出 A+B. #include <iostream> using ...