Leetcode 笔记 98 - Validate Binary Search Tree

时间:2022-09-03 21:04:11

题目链接: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&period; Validate Binary Search Tree &lpar;2 solutions&rpar;

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

  2. 【一天一道LeetCode】&num;98&period; Validate Binary Search Tree

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

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

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

  4. LeetCode OJ 98&period; 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&period; 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. &lbrack;LeetCode&rsqb; 98&period; 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. android微信聊天记录导出到电脑【微信安卓版技巧】

    微信,对它又爱又恨!爱的是微信能替代很多手机通话短信,恨的是有些较早前的手机不能友好支持,比如ytkah之前用的i8000,挺上手的,就是没办法装微信,当时工作需要必须用微信,只好忍痛割爱买了个and ...

  2. mysql数据库常规命令操作

    1.MySQL数据库导出命令 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名     mysqldump -u wcnc -p smgp_apps_wcn ...

  3. c语言中的fgets函数

    fgets()函数用于从文件流中读取一行或指定个数的字符,其原型为: char * fgets(char * string, int size, FILE * stream); 参数说明:string ...

  4. CSS 基本知识

    1.CSS 简介 CSS 指层叠样式表 (Cascading Style Sheets),是一种用来表现 HTML 文档样式的语言,样式定义如何显示 HTML 元素,是能够真正做到网页表现与结构分离的 ...

  5. android学习——ADT的离线安装

    前一篇讲解了ADT的在线安装,不过有的时候在线在线安装的速度很慢.所以今天学习一下ADT的离线安装: 首先 下载与SDK相对应的ADT(如果SDK是最新版的就下最新版ADT否则就把SDK更新到最新版以 ...

  6. react 各种UI框架

    共计bfd-ui,react-amaze-ui,react-ant-design,react-material-ui,react-components,react-desktop,react-ui,s ...

  7. 没有基础如何学习web渗透测试?

    如果只是因为感兴趣玩玩的话,你可以不需要学习太多的脚本程序. 但是以下条件要具备 1.能看懂教程:能理解原理,例如解析漏洞,sql注入逻辑等 2.前端代码审计:html js css 3.主流工具的使 ...

  8. PHP正则匹配与文件编码关系

    虽然多数高手认为正则会影响程序效率,但是做数据采集的时候,却很难避免使用正则, 强大的正则表达式用起来很舒服,但是在匹配中文的时候,会出现,明明正则表达式没问题,字符数据里包含符合正则表达式的数据,可 ...

  9. Linux 问题

    Loaded plugins: fastestmirror cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.repo.backup wget h ...

  10. Android Studio 创建不恰当的虚拟设备导致程序不正常运行

    操作系统:Windows 10 x64 IDE:Android Studio 3.2.1 使用Android Studio新建第一个Android程序,一开始在虚拟设备上面调试,不管程序怎么修改,运行 ...