给定一个二叉树,判断其是否是一个有效的二叉搜索树。
一个二叉搜索树有如下定义:
    左子树只包含小于当前节点的数。
    右子树只包含大于当前节点的数。
    所有子树自身必须也是二叉搜索树。
示例 1:
    2
   / \
  1   3
二叉树[2,1,3], 返回 true.
示例 2:
    1
   / \
  2   3
二叉树 [1,2,3], 返回 false.
详见:https://leetcode.com/problems/validate-binary-search-tree/description/

Java实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
if(root==null){
return true;
}
return isValidBST(root,Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean isValidBST(TreeNode root,long min,long max){
if(root==null){
return true;
}else if(root.val<=min||root.val>=max){
return false;
}
return isValidBST(root.left,min,root.val)&&isValidBST(root.right,root.val,max);
}
}

参考:https://www.cnblogs.com/grandyang/p/4298435.html

098 Validate Binary Search Tree 验证二叉搜索树的更多相关文章

  1. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

  2. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

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

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  5. Leetcode98. Validate Binary Search Tree验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...

  6. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  7. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  8. [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树

    判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...

  9. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. 发布镶嵌数据集,服务端Raster Function制作

    1.新建GDB 2.GDB里右键,New 镶嵌数据集. 3.向镶嵌数据集中添加风速TIF. 4.利用原样式,导出一个函数模板. 导出是XML. 5.发布ImageService服务时,在Functio ...

  2. Linux各类压宿包的解压方法

    01-.tar格式解包:[*******]$ tar xvf FileName.tar 打包:[*******]$ tar cvf FileName.tar DirName(注:tar是打包,不是压缩 ...

  3. php排序方法之插入排序

    //插入排序法 $arr = array(3,55,45,2,67,76,6.7,-65,85,4); function insertSort($arr){ for ( $i=0; $i<cou ...

  4. 【转载】如何在 C#中访问 JavaScript函数?

    如何在 C#中访问 JavaScript函数? 时间:13-10-17 栏目:Unity3D教程 作者:zqcyou 评论:0     如何在 C#中访问 JavaScript函数?答案如下:c#代码 ...

  5. Nginx基本配置和作用

    nginx可以重新加载文件的.我们直接运行:nginx -s reload 配置文件有没有问题,可以直接输入:nginx -t nginx -s stop就可以关闭 但有时我们就不想它挂的时候访问另外 ...

  6. 计时器 vb

    十分钟 我们寒假就是这么长!! vb 执行cmd   :              shell "cmd /c DOS命令" vb 取系统日期: Print Date; " ...

  7. A - Vasya and Socks

    A - Vasya and Socks Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  8. python--flask学习1

    1 windows/unix得安装 http://www.pythondoc.com/flask-mega-tutorial/helloworld.html http://www.pythondoc. ...

  9. c# 调用 webservices (转载)

    .net 调用webservice 总结 最近做一个项目,由于是在别人框架里开发app,导致了很多限制,其中一个就是不能直接引用webservice . 我们都知道,调用webserivice 最简单 ...

  10. C#基础之--线程、任务和同步:一、异步委托

    创建线程的一种简单方式是定义一个委托,并异步调用它.委托是方法的类型安全的引用. Delegate还支持异步地调用方法.在后台Delegate类会创建一个执行任务的线程. 为了说明委托的异步特性,从一 ...