【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 ...
随机推荐
- 用三目运算,与if判断 函数调用 达到相同判定作用
三目运算符: 操作数1 ? 操作数2 : 操作数3 (操作数1位bool类型,操作数2和操作数3为两个相同的任何类型) 返回结果:如果操作数1判定结果为真,则将操作数2作为返回结果如果操作 ...
- (转)linux 命令访问url: curl http://www.baidu.com/index.html
转:https://blog.csdn.net/michael1112/article/details/79119424 1.elinks - lynx-like替代角色模式WWW的浏览器 例如: e ...
- Anaconda在Python3和Python2之间切换,Conda命令,anaconda中python的升级和降级
当在pycharm IDE中指定不同的Python版本时,设置方法 File->Setting->Project:XXXX->Project Interpreter 选择不同位 ...
- Integer自动装箱和拆箱
Integer a=3; => Integer a=Integer.valueOf(3); /** *@description: 自动装箱和拆箱 *@auther: yangsj *@ ...
- 网络设备MIB浏览器ifType、ifDescr、ifMtu、ifInOctets等的含义(Zabbix SNMP)
1.ifType 接口的类型 取值117表示接口为GigabitEthernet (取值62表示接口为 FastEnthernet) 2.ifDescr 接口类型的描述 有GigabitEtherne ...
- GARENA笔试sql20190926
create database garena; use garena; create table players( account_id int, name varchar(20), country ...
- java.lang.IllegalStateException: Cannot forward after response has been committed
jjava.lang.IllegalStateException: Cannot forward after response has been committed at org.apache.cat ...
- P2747 [USACO5.4]周游加拿大Canada Tour
题目描述 你赢得了一场航空公司举办的比赛,奖品是一张加拿大环游机票.旅行在这家航空公司开放的最西边的城市开始,然后一直自西向东旅行,直到你到达最东边的城市,再由东向西返回,直到你回到开始的城市.除了旅 ...
- Zookeeper——启动闪退
Zookeeper好久不启动了,昨天项目要用Zookeeper了,我昨天突然启动它,调皮的zk居然害羞不让我看见它,启动不了,一启动就闪退,为啥呢?其实是因为报错了,有错zk启动时就会报错,所以昨 ...
- 3. ZooKeeper客户端(一)
ZooKeeper常用客户端有三种:原生客户端.zkClient.curator 项目中使用前,需要导入相关依赖 <dependencies> <dependency> < ...