【leetcode】1008. Construct Binary Search Tree from Preorder Traversal
题目如下:
Return the root node of a binary search tree that matches the given
preordertraversal.(Recall that a binary search tree is a binary tree where for every node, any descendant of
node.lefthas a value<node.val, and any descendant ofnode.righthas a value>node.val. Also recall that a preorder traversal displays the value of thenodefirst, then traversesnode.left, then traversesnode.right.)Example 1:
Input: [8,5,1,7,10,12]
Output: [8,5,10,1,7,null,12]
![]()
Note:
1 <= preorder.length <= 100- The values of
preorderare distinct.
解题思路:以用例的输入[8,5,1,7,10,12]为例,很显然8是根节点,8的左子树有[5,1,7],右子树右[10,12],左右子树的分割点是后面第一个比根节点大的数。接下来再分别对[5,1,7]和[10,12]做同样的操作,可以知道5是8的左子树根节点,1和7分别在其左右;而10是8的右子树根节点,12为右子树节点。很显然这是一个递归的过程,只要找到每个子树的根节点将其左右子树划分即可。
代码如下:
# 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 build(self,node,preorder):
if len(preorder) == 0:
return
left = []
for i in range(len(preorder)):
if node.val < preorder[i]:
break
else:
left.append(preorder[i])
right = preorder[len(left):]
if len(left) >= 1:
node.left = TreeNode(left.pop(0))
self.build(node.left,left)
if len(right) >= 1:
node.right = TreeNode(right.pop(0))
self.build(node.right,right)
def bstFromPreorder(self, preorder):
"""
:type preorder: List[int]
:rtype: TreeNode
"""
root = TreeNode(preorder.pop(0))
self.build(root,preorder)
return root
【leetcode】1008. Construct Binary Search Tree from Preorder Traversal的更多相关文章
- 【LeetCode】1008. Construct Binary Search Tree from Preorder Traversal 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】 99. Recover Binary Search Tree [Hard] [Morris Traversal] [Tree]
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- LeetCode 1008. Construct Binary Search Tree from Preorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 题目: Retu ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...
- 【LeetCode】98. Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode】99. Recover Binary Search Tree
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [Swift]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that ...
随机推荐
- mysql主从架构,IO、SQL线程运行为YES,从库没有同步数据
mysql基于binlog主从复制架构,IO.SQL线程运行为YES,从库没有同步数据 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_D ...
- 学习使用Delphi for android 调用Java类库
http://blog.csdn.net/laorenshen/article/details/41148253 学习使用Delphi for android 调用Java类库 2014-11-15 ...
- indy idhttpserver有关下载的两个问题
http://aawwmate.blog.163.com/blog/static/77528256201092733950315/ indy idhttpserver有关下载的两个问题 2010-10 ...
- RSA - 原理、特点(加解密及签名验签)及公钥和私钥的生成
Wiki - RSA加密演算法 Wiki - 欧拉函数 Wiki - 模反元素 ASN.1 格式标准 RSA算法原理(二) 注意: RSA 加密或签名后的结果是不可读的二进制,使用时经常会转为 BAS ...
- 【转载】Spring boot学习记录(二)-配置文件解析
前言:本系列文章非本人原创,转自:http://tengj.top/2017/04/24/springboot0/ 正文 Spring Boot使用了一个全局的配置文件application.prop ...
- 16/7/9_Bootstrap-设计原则
移动优先: • 在设计的初期就要考虑页面如何在多终端展示 渐进增强: • 充分发挥硬件设备的最大功能
- python实现读取excel
实现代码如下: #读取excel,将每行数据放入一个列表,将所有列表放入一个列表形成二维列表,返回该二维列表 import xlrd class ReadExcel: def read_excel(s ...
- Colourful Rectangle【扫描线】
题目链接 很明显的可以发现是一个扫描线的问题,但是怎么处理区域呢,发现只有三种颜色,也就是最多也就是7种状态,那么我们可以进行一个状态压缩即可. 但是,在向上pushup的时候,存在我们要以子树的状态 ...
- C#获取本地路径
/// <summary> /// 本地路径 /// </summary> /// <param name="path"></param& ...
- Easyui表格显示日期格式错误
问题 如下图: 解决办法: 以Sql Server 2008为例,在执行查询时,把“采购日期”字段由datetime转换为string,前端以string类型显示.