题目链接:Validate Binary Search Tree | LeetCode OJ

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.

Tags: Tree, Depth-first Search

分析

读题!读题!读题!这道题再次提醒我们要把读题这两个字默读100遍。

对于Binary Search Tree,每道题都需要认真确认题中的约定是否与自己的理解相符,比如大名鼎鼎的Cracking the Coding Interview中,对于二叉查找树的定义为“左子结点小于或等于当前结点”,本题中的描述为”左子结点小于当前结点“。把题目中对二叉查找树的要求总结如下:

  • 左子树中的结点均小于当前结点
  • 右子树中的结点均大于当前结点
  • 左子树和右子树本身也必须是二叉查找树(意味着左子树的所有子结点均不能大于当前结点,右子树的所有子结点均不能小于当前结点)

示例

class Solution:
# @param root, a tree node
# @return a boolean
def isValidBST(self, root):
return self._isValidBST(root, None, None); def _isValidBST(self, root, min, max):
if root is None or root.val is None:
return True if (min is not None and root.val <= min) or (max is not None and root.val >= max):
return False return self._isValidBST(root.left, min, root.val) and self._isValidBST(root.right, root.val, max)

Leetcode 笔记系列的Python代码共享在https://github.com/wizcabbit/leetcode.solution

示例说明

  • 示例中初始节点的最大、最小值传入了None,理论上应该传入int类型的最小、最大值,但由于python中传入int的极值需要import sys,或者使用硬编码的(-2147483647, 2147483647)。我不喜欢在算法题中引入系统库或者硬编码数字,因此使用了None来代表极值;
  • 为了代码可读性没有简化判断语句,可以精简为:
class Solution:
# @param root, a tree node
# @return a boolean
def isValidBST(self, root):
return self._isValidBST(root, -2147483647, 2147483647); def _isValidBST(self, root, min, max):
if root is None or root.val is None:
return True return root.val < max \
and root.val > min \
and self._isValidBST(root.left, min, root.val) \
and self._isValidBST(root.right, root.val, max)

相关题目

Recover Binary Search Tree

Leetcode 笔记 98 - Validate Binary Search Tree的更多相关文章

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

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

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

  3. 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...

  4. LeetCode OJ 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

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  6. leetcode笔记:Validate Binary Search Tree

    一. 题目描写叙述 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is ...

  7. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  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] 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 ...

随机推荐

  1. Java MyBatis 插入数据库返回主键

    最近在搞一个电商系统中由于业务需求,需要在插入一条产品信息后返回产品Id,刚开始遇到一些坑,这里做下笔记,以防今后忘记. 类似下面这段代码一样获取插入后的主键 User user = new User ...

  2. MIP开发常见问题解答

    校验相关 1. MIP 页面的 <a>链接校验报错,MIP 是强制 target="_blank" 吗? 如果想直接跳转MIP页,可以用mip-link 组件:MIP ...

  3. C语言 · 薪水计算

    问题描述 编写一个程序,计算员工的周薪.薪水的计算是以小时为单位,如果在一周的时间内,员工工作的时间不超过40 个小时,那么他/她的总收入等于工作时间乘以每小时的薪水.如果员工工作的时间在40 到50 ...

  4. input[tyle="file"]样式修改及上传文件名显示

    默认的上传样式我们总觉得不太好看,根据需求总想改成和上下结构统一的风格…… 实现方法和思路: 1.在input元素外加a超链接标签 2.给a标签设置按钮样式 3.设置input[type='file' ...

  5. 谱聚类(spectral clustering)原理总结

    谱聚类(spectral clustering)是广泛使用的聚类算法,比起传统的K-Means算法,谱聚类对数据分布的适应性更强,聚类效果也很优秀,同时聚类的计算量也小很多,更加难能可贵的是实现起来也 ...

  6. C++11特性——变量部分(using类型别名、constexpr常量表达式、auto类型推断、nullptr空指针等)

    #include <iostream> using namespace std; int main() { using cullptr = const unsigned long long ...

  7. [EasyUI美化换肤]更换EasyUi图标

    前言 本篇文章主要是记录一些换EasyUI皮肤的过程,备忘.也欢迎美工大神各路UI给点好意见,EasyUI我就不介绍了,自行百度吧..(So..所以别问我是不是响应式..本身EasyUI就不是响应式. ...

  8. Javascript高性能编程-提高Dom访问速度

    在浏览器中对于Dom的操作和普通的脚本的操作处于两个不同的dll中,两个dll的交互是比较耗时的,优化对Dom的操作可以提高脚本的执行速度.下面是对如何优化的一些总结: 将需要多次操作的节点存储在一个 ...

  9. DevExpress - 使用 GaugeControl 标尺组件制作抽奖程序 附源码

    前不久,公司举办了15周年庆,其中添加了一个抽奖环节,要从在读学员中随机抽取幸运学员,当然,这个任务就分到了我这里. 最后的效果如下,启动有个欢迎页面,数据是来自Excel的,点击开始则上面的学号及姓 ...

  10. windows 7(32/64位)GHO安装指南(系统安装篇)~重点哦!!~~~~

    经过了前三篇的铺垫,我们终于来到了最重要的部分~~如果没看过前几篇的小伙伴们,可以出门右转~~用十几分钟回顾一下~~然后在看这篇会感觉不一样的~~~~ 下面让我们来正式开始吧 我们进入大白菜的桌面是酱 ...