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.

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

判断一棵二叉树是否是二叉搜索树,我们可以采用中序遍历来看二叉树的中序遍历是否是递增的,如果是递增的,那么就是BST,如果不是递增的,那么就不是BST。

代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public long n = (long)Integer.MIN_VALUE-1;
public boolean isValidBST(TreeNode root) {
if(root == null) return true;
boolean a = isValidBST(root.left);
if(root.val<=n) return false;
else n = root.val;
boolean b = isValidBST(root.right);
return (a&&b);
}
}

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

  1. Leetcode 笔记 98 - Validate Binary Search Tree

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

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

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

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

  4. LeetCode OJ: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 解题报告(Python & C++ & Java)

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

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

  7. 【LeetCode OJ】Validate Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the ...

  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. FR javascript 时间设置上个月最后一天后当月最后一天

    //设置上月最后一天 var date1 =new Date(); date1.setDate(1); //first day; date1.setMonth(date1.getMonth()); / ...

  2. C#动态编程

    class Program { static void Main(string[] args) { Test(); } public static void Test() { //声明代码的部分 Co ...

  3. java.lang.OutOfMemoryError: Java heap space错误及处理办

    默认方式启动Eclipse时,有关启动时JVM参数是在Eclipse安装目录下的eclipse.ini文件中指定的.在命令行下,也可以通过参数-vmargs来达到此目的.其命令格式为:eclipse ...

  4. sublime 3 增加php开发插件

    1.PHP语法自动完成插件 https://github.com/erichard/SublimePHPCompanion 2.ThinkPHP自动完成插件 https://github.com/ya ...

  5. CodeForces 702D Road to Post Office

    答案的来源不外乎于3种情况: 纯粹走路,用时记为${t_1}$:纯粹乘车,用时记为${t_2}$:乘车一定距离,然后走路,用时记为${t_3}$. 但是${t_1}$显然不可能成为最优解. 前两个时间 ...

  6. URAL 1525 Path

    #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> us ...

  7. 第六十八节,htnl全局属性和其他属性

    htnl全局属性和其他属性 学习要点: 1.实体 2.元数据 3.全局属性 本章主要探讨HTML5中的HTML实体.以及HTML核心构成的元数据,最后了解一下HTML中的全局属性. 一.实体 HTML ...

  8. Java 多态 父类和子类方法的访问控制权限

    Java 多态 父类和子类方法的访问控制权限 @author ixenos 父类和子类方法的访问控制权限 继承是为了扩展类的功能,而这种扩展显然就是对一个原始类的扩展,目的还是向上转型来调用,所以这就 ...

  9. 1、Spring概述

    Java EE优缺点 我们都知道在2003年Spring兴起之前,企业普遍使用J2EE技术来开发企业级应用,为什么用J2EE呢?主要原因有以下几个: 1.Java本身的跨平台能力,可移植性强2.J2E ...

  10. MySQL事务内幕与ACID

    MySQL的事务实现严格遵循ACID特性,即原子性(atomicity),一致性(consistency),隔离性(isolation),持久性(durability).为了避免一上来就陷入对ACID ...