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.

题解:BST valid 的充分必要条件是它的中序遍历是一个有序序列。

递归实现树的中序遍历,用私有变量lastVal记录上一个遍历的节点的值。在一次递归,首先递归判断左子树是否是BST,并且更新lastVal,然后将root的值跟lastVal比较,看root的值是否大于lastVal;然后递归判断右子树是否是BST。

代码如下:

 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private int lastVal = Integer.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if(root == null)
return true; if(!isValidBST(root.left))
return false; if(root.val <= lastVal)
return false;
lastVal = root.val;
if(!isValidBST(root.right))
return false;
return true;
}
}

题目的关键点是lastVal更新的时机和与root比较的时机。

【leetcode刷题笔记】Validate Binary Search Tree的更多相关文章

  1. LeetCode之“树”:Validate Binary Search Tree

    题目链接 题目要求: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is ...

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

  3. LeetCode算法题-Trim a Binary Search Tree(Java实现)

    这是悦乐书的第284次更新,第301篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第152题(顺位题号是669).给定二叉搜索树以及L和R的最低和最高边界,修剪树以使其所 ...

  4. 【leetcode刷题笔记】Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  5. 【leetcode刷题笔记】Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  6. 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  7. 【leetcode刷题笔记】Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  8. 【leetcode刷题笔记】Word Search

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

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

  10. 【LeetCode练习题】Validate Binary Search Tree

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

随机推荐

  1. solr查询

    1.根据字段查询: http://www.360doc.com/content/14/0306/18/203871_358295621.shtml 2.模糊查询: http://www.tuicool ...

  2. ios - 使用@try、catch捕获异常:

    @try { // 可能会出现崩溃的代码 } @catch (NSException *exception) { // 捕获到的异常exception } @finally { // 结果处理 }

  3. Android自定义View分析

    一.基本步骤 1.自定义View的属性 2.在View的构造方法中获取自定义属性 3.重写onMesure方法(非必须) 4.重写onDraw方法 二.具体实现 1.自定义View的属性,首先在res ...

  4. 已备份数据库的磁盘结构版本号为611,server支持版本号为539,无法还原或升级数据库

    提供的是bak文件是2005备份的,还原到本地的sqlserver2000,提示:已备份数据库的磁盘上结构版本号为611.服务器支持版本号539,无法还原或升级数据库. 网上找了下,原因是611是sq ...

  5. Windows7 配置两个版本的java环境,可自由切换

    1. 准备工作 下载jdk: jdk1.7[http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads ...

  6. COGS 1507. [IOI2000]邮局

    1507. [IOI2000]邮局 ★☆   输入文件:postoffice.in   输出文件:postoffice.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] ...

  7. 1 了解Scala

    1 定义变量 单个变量:var name = "benxintuzi" 等价于  var name : String = "benxintuzi"(即定义变量时 ...

  8. iOS和Android后台机制对比

    转自:http://blog.csdn.net/zsch591488385/article/details/27232881 一.iOS的“伪后台”程序 首先,先了解一下ios 中所谓的「后台进程」到 ...

  9. UIApplicationDelegate 各方法回调时机

    本篇文章主要介绍一些UIApplicationDelegate中几个常用的回调方法的调用时机.以帮助你判断哪些方法倒底放到哪个回调中去实现. 1. – (void)applicationDidFini ...

  10. R语言读取Excel文档

    在R语言数据管理(三):数据读写一博文中,我曾写到有关读取xls.xlsx文件时一般将文档改成csv文件读取,这是一般做法.csv文件也有其缺点,修改较为麻烦,当文件数据较大时尤为明显.而生活中必不可 ...