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:

    2
/ \
1 3

Binary tree [2,1,3], return true.

Example 2:

    1
/ \
2 3

Binary tree [1,2,3], return false.

对每一个节点使用一个upper bound and lower bound. 时间复杂度 O(N).

 def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
maxInt = 2147483647
minInt = -2147483648 return self.isBSTHelper(root, minInt, maxInt) def isBSTHelper(self, root, minVal, maxVal):
if not root:
return True if root.val <= maxVal and root.val >= minVal and self.isBSTHelper(root.left, minVal, root.val - 1) and self.isBSTHelper(root.right, root.val + 1, maxVal):
return True
else:
return False

思路二:如果中序遍历一下tree,BST应该给出一个递增序列。注意中序遍历的非递归算法。

 class Solution(object):
def isValidBST(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
ans = []
self.inOrder(root, ans) n = len(ans)
if n <= 1:
return True
for i in range(1,n):
if ans[i] <= ans[i-1]:
return False
return True def inOrder(self, root, ans):
if not root:
return ans
else:
self.inOrder(root.left, ans)
ans.append(root.val)
self.inOrder(root.right, ans)

Leetcode 98. Validate Binary Search Tree的更多相关文章

  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 ----- java

    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验证二叉搜索树

    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(是否是二叉搜索树) ☆☆☆

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

  5. Leetcode 笔记 98 - Validate Binary Search Tree

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

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

  7. 【leetcode】Validate Binary Search Tree

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

  8. leetcode dfs Validate Binary Search Tree

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

  9. 【一天一道LeetCode】#98. Validate Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. webstrom软件使用

    很多人都发现 http://idea.lanyus.com/ 不能激活了 很多帖子说的 http://15.idea.lanyus.com/ 之类都用不了了 选择 License server (20 ...

  2. 升级nodejs版本

    node有一个模块叫n,是专门用来管理node.js的版本的. 首先安装n模块: npm install -g n 第二步: 升级node.js到最新稳定版 n stable n后面也可以跟随版本号比 ...

  3. 清北学堂2017NOIP冬令营入学测试P4749 F’s problem(f)

    时间: 1000ms / 空间: 655360KiB / Java类名: Main 背景 冬令营入学测试 描述 这个故事是关于小F的,它有一个怎么样的故事呢. 小F是一个田径爱好者,这天它们城市里正在 ...

  4. WampServer下如何实现多域名配置(虚拟域名配置)

    之前在学习跨域的时候,我写过一篇叫做WampServer下使用多端口访问的文章,默认的 localhost 采用的是 80 端口,能使用多端口访问的核心是得新建一个端口,也就是新建一个 http 服务 ...

  5. HTTP Header详解(转载)

    HTTP Header详解 HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服务 ...

  6. [BZOJ1264][AHOI2006]Match(DP+树状数组)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1264 分析: 考虑做一般的LCS的时候,更新结果的条件是a[i]==b[j]时候 于是 ...

  7. Matlab生成M序列的伪随机码

    伪随机编码中较常用的是m序列,它是线性反馈移位寄存器序列的一种,其特点是在相同寄存器级数的情况下输出序列周期最长.线性反馈移位寄存器的工作原理是,给定所有寄存器一个初始值,当移位脉冲到来时,将最后一级 ...

  8. spring+mybaties+springMvc+slf4j所需jar包

  9. Linux配置VNC实现远程图形化操纵

    问题描述 有些时候需要用到图形化,其实可以通过其他途径实现.但是懒惰的就喜欢VNC,总的老说都是需要图形组件的 问题解决 在Centos测试 一.图形化的Linux 01.安装 rpm  ivh vn ...

  10. 1415-2 计科&计高 软件工程博客&Github地址汇总-修正版

    序号 标识 博客 代码 1 1121袁颖 joanyy joanyy 2 1122崔琪 chitty ChittyCui 3 1123吕志浩 lucy123 715lvzhihao 4 1124张静 ...