【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 ...
随机推荐
- C++ 运算符优先级顺序表 (最新/完整)
优先级 运算符 结合律 助记 1 :: 从左至右 作用域 2 a++.a--.type().type{}.a().a[]...-> 从左至右 后缀自增减.函数风格转型.函数调用.下标.成员访问 ...
- python中的方法使用
#Python其实有3个方法,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下: class Foo: def bar(self): # cls 是当前对象的实 ...
- 015-Spring Boot 定制和优化内嵌的Tomcat
一.内嵌web容器 参看http://www.cnblogs.com/bjlhx/p/8372584.html 查看源码可知提供以下三种: 二.定制优化tomcat 2.1.配置文件配置 通过appl ...
- DR 项目小结
前言 个人的项目总结, 非技术类博文. 需要补充的知识点 HTTP 协议与其内置方法 curl 指令和各选项的意义 Keystone 认证流程和各项目配置文件 [keystone_authtoken] ...
- Gitlab仓库搭建和免密使用gitlab
Gitlab简介 GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 可通过Web界面进行访问公开的或者私人项目.它拥有与Github类似的 ...
- delphi idhttpsever
http://blog.csdn.net/chelen_jak/article/details/50203809 delphi idhttpsever 2015-12-07 11:36 216人阅读 ...
- Jmeter接口自动化培训
课程前提 速成班,讲的不会非常深,每个人基础不一样,但是实现接口自动化没有问题,因为jmeter更多的用来做性能测试.当然有兴趣我们也可以穿插一点 课程基本大纲 接口基础概念 部署本地测试环境(使用d ...
- Xpath表达式的粗介绍
关于在自动化中Xpath表达式的书写,其实我也只是刚刚入门,粗略的跟着网上的教程学了一下,这篇我就来分享总结一下我学习到的知识. 首先呢,我们先认识一下什么是Xpath.Xpath是XML路径语言,它 ...
- 网络流强化-POJ2516
k种货物分开求解最小费用最大流,主要减少了寻找最短路的时间. #include<queue> #include<cstdio> #include<cstring> ...
- Play with Chain 【HDU - 3487】【Splay+TLE讲解】
题目链接 很好的一道题,用了三天多的时间,终于知道了我为什么T的原因,也知道了在Splay的同时该怎样子的节约时间,因为Splay本身就是大常数的O(N*logN),我们如果不在各种细节上节约时间,很 ...