Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

Input:
2
/ \
1 3
Output: true

Example 2:

    5
/ \
1 4
  / \
  3 6
Output: false
Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value
  is 5 but its right child's value is 4. 我们用LeetCode questions conlusion_InOrder, PreOrder, PostOrder traversal里面inorder的思路和code, 只需要判断是否ans是升序即可.
04/20/2019 Update : add one more solution using Divide and conquer. (add a class called returnType)
1. recursive S: O(n)
class Solution:
def validBST(self, root):
def helper(root):
if not root: return
helper(root.left)
ans.append(root.val)
helper(root.right)
ans = []
helper(root)
for i in range(1, len(ans)):
if ans[i] <= ans[i-1]: return False
return True

2. iterable     S; O(n) . 因为有stack

class Solution:
def validBST(self, root):
stack, pre = [], None
while stack or root:
if root:
stack.append(root)
root = root.left
else:
node = stack.pop()
if pre != None and node.val <= pre:
return False
pre = node.val
root = node.right
return True

3. Divide and Conquer

class ResultType:
def __init__(self, isBST, minVal, maxVal):
self.isBST = isBST
self.minVal = minVal
self.maxVal = maxVal class Solution:
def validBST(self, root):
def helper(root):
if not root:
return ResultType(True, float('inf'), float('-inf'))
left = helper(root.left)
right = helper(root.right)
if not left.isBST or not right.isBST or (root.left and root.val <= left.maxVal) or (root.right and root.val >= right.minVal):
return ResultType(False, 0, 0)
return ResultType(True, min(root.val, left.minVal), max(root.val, right.maxVal))
return helper(root).isBST

4. S: O(1)

class Solution:
def validBST(self, root):
ans = [None, True] # pre node, answer def helper(root, ans):
if not root: return
helper(root.left, ans)
if ans[1] and ans[0] and ans[0].val >= root.val:
ans[1] = False
ans[0] = node
helper(root.right, ans) helper(root, ans)
return ans[1]

[LeetCode] 98. Validate Binary Search Tree_Medium的更多相关文章

  1. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  2. Leetcode 98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. leetcode 98 Validate Binary Search Tree ----- java

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  4. [leetcode]98. Validate Binary Search Tree验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. [LeetCode] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆

    描述 解析 二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n. 节点n的右孩子所在的树,每个节点都大于节点n. 定义子树的最大最小值 比如:左孩子要小于父节点:左孩子n的右孩子要大于n ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. 【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) ...

  8. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  9. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

随机推荐

  1. 查找C++代码中某一范围内的内存泄露

    #include <string.h> #define _CRTDBG_MAP_ALLOC #include <crtdbg.h> int _tmain(int argc, _ ...

  2. C# MVC+EF—结构搭建

    近期做了MVC+EF的项目,现在项目完结了,抽个时间写个小DOM总结一下,顺便加深理解. 一.新建MVC项目,结构是这样的

  3. DB2 的代理 (agent)

    DB2 的代理 (agent) 是位于 DB2 服务器中的服务于应用程序请求的一些进程或线程.当有外部应用程序连接至 DB2 实例提出访问请求时,DB2 的代理就会被激活去应答这些请求.一般 DB2 ...

  4. db2 查杀死锁进程

    db2 查杀死锁进命令 db2 get snapshot for locks on (需要snapshot的访问权限) db2 list applications db2 "force ap ...

  5. mysql索引及sql执行顺序

    1, 红黑树 同一层级的黑树到根结点经历的黑树数目一样 最坏情况的时间复杂度 lg n 是二叉树b树 结点可以有多个孩子 b+树 父节点不存储数据聚集索引)的叶子节点会存储数据行,也就是说数据和索引是 ...

  6. ubuntu经常断网、掉线、上不去网的原因

    方案一: ubuntu经常断网.掉线.上不去网的原因,很可能是因为IPv6的关系,Ubuntu默认开启IPv6,在“设置--wifi--齿轮图标”中关掉就可以了. 经我环境测试,此方法无效 方案二: ...

  7. hive优化之调整mapreduce数目

    一.调整hive作业中的map数 1.通常情况下,作业会通过input的目录产生一个或者多个map任务.主要的决定因素有: input的文件总个数,input的文件大小,集群设置的文件块大小(目前为1 ...

  8. hide server info

    <?php /*wamp64\bin\apache\apache2.4.18\confhttpd.conf ServerSignature On  ServerTokens Full Serve ...

  9. elasticsearch in docker/ and aggregation,,performance tune ;throughout

    Docker环境中Elasticsearch的安装 ]https://wenchao.ren/archives/category/elasticsearch/page/2 [ElasticSearch ...

  10. 终端:Xcode模拟器上安装.app方法

    有的时候,我们可能需要将别人的Xcode运行之后的程序包(xxx.app)安装在自己的模拟器上,如下我将介绍如何通过终端来安装. 实现 获取自己Xcode生成的xxx.app steps 1:在工程d ...