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. 用nodejs搭建一个简单的服务监听程序

    作为一个从业三年左右的,并且从事过半年左右PHP开发工作的前端,对于后台,尤其是对以js语言进行开发的nodejs,那是比较有兴趣的,虽然本身并没有接触过相关的工作,只是自己私下做的一下小实验,但是还 ...

  2. logback logback.xml常用配置详解(一)<configuration> and <logger>

    logback logback.xml常用配置详解(一)<configuration> and <logger> 博客分类: Log java loglogback  原创文章 ...

  3. windous----操作系统基础

    操作系统基础 服务软件,控制硬件. 一:什么事操作系统 操作系统就是一个协调,管理和控制和计算机硬件资源控制程序.  用户态:运行应用程序,不可以操作硬件(可以获取cpu的指令集的一个子集,该子集不包 ...

  4. Linux关闭IPV6

    Linux关闭IPV6的方法 修改配置文件/etc/sysctl.conf添加以下1行 net.ipv6.conf.all.disable_ipv6 = 1 设置生效 sysctl -p 查看没有IP ...

  5. 关于ti环境搭建 IAR 为什么IAR不能跳转的真正原因

    1.IAR工程莫名其妙的错误,可以通过复制工程文件来解决 2.cc2540环境配置,尽量默认到c盘,以便解决不必要的麻烦,另外,不同的库尽量安装对应的环境文件,比如freertos等环境 3.对于蓝牙 ...

  6. linux 背单词

    命令缩写: ls:list(列出目录内容) cd:Change Directory(改变目录) su:switch user 切换用户rpm:redhat package manager 红帽子打包管 ...

  7. JSP+Servlet+JavaBean实现数据库的增删改查

    基本思想:JSP文件显示页面,使用form或href超链接传值到Servlet中方法,在Servlet方法中调用Dao层的类对象,实现对数据库里的数据的增删改查,之后重新返回到JSP输出操作完的结果. ...

  8. Android 屏蔽Power键 Home键

    Android 屏蔽Power键 Home键 标签: androidpower键Home键PhoneWindowManagerframework 2014-04-18 17:22 1557人阅读 评论 ...

  9. 知乎如何基于开源Druid打造下一代数据平台?

    https://mp.weixin.qq.com/s/WXA_Pn5xwlL-cA4JaGRw6Q

  10. MonkeyRunner_手机触摸屏幕坐标获取

    坐标单位为像素,获取方式包括手机自带.画图软件和photoshop软件. 方式一.android4.0以上系统手机自带坐标功能,在设置->开发者选项->显示指针位置 方式二.画图或phot ...