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. Intersection Observer API 可以让你知道被观察元素何时进入或退出浏览器的视口

    google 文档 https://developers.google.cn/web/updates/2016/04/intersectionobserver MDN 文档 https://devel ...

  2. h5 . css入门 2.CSS基础

    CSS基础 学习目标 1.CSS简介 2.CSS语法 3.样式的创建 4.两种引入外部样式表的区别 5.样式表的优先级和作用域 6.CSS选择器 7.选择器的权重 8.浮动属性的简单应用 9.HTML ...

  3. 【吴恩达课后测验】Course 1 - 神经网络和深度学习 - 第一周测验【中英】

    [吴恩达课后测验]Course 1 - 神经网络和深度学习 - 第一周测验[中英] 第一周测验 - 深度学习简介 和“AI是新电力”相类似的说法是什么? [  ]AI为我们的家庭和办公室的个人设备供电 ...

  4. TOP100summit2017:网易测试总监钱蓓蕾——新时代测试正走向精英化、自动化、智能化

    壹佰案例:钱蓓蕾老师您好,很荣幸邀请到您成为第六届壹佰案例峰会测试专场的联席主席.您从事测试工作11年了,您觉得最近几年测试工作有什么趋势上的变化么? 钱蓓蕾:测试行业近几年变化挺大的,正逐渐向精英化 ...

  5. java虚拟机学习

    //20181129 ·Java虚拟机的内存分为三个部分:栈stack.堆heap.方法区method area----包含在“堆”里面,因为作用特殊所以单独列出来 ·栈的特点:     栈描述的是方 ...

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

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

  7. mysql缓存分析

  8. LeetCode 500 Keyboard Row 解题报告

    题目要求 Given a List of words, return the words that can be typed using letters of alphabet on only one ...

  9. ECharts图形库

    ECharts图形库百度的项目,图形的创建也比较简单,直接引用Javascript即可 1,引入<script src="{{ url_for("static",f ...

  10. 根据xml配置使用反射动态生成对象

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...