给定一个二叉树,判断其是否是一个有效的二叉搜索树。
一个二叉搜索树有如下定义:
    左子树只包含小于当前节点的数。
    右子树只包含大于当前节点的数。
    所有子树自身必须也是二叉搜索树。
示例 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. CyclicBarrier及CountDownLatch的使用

    CountDownLatch位于java.util.concurrent包下,是JDK1.5的并发包下的新特性. 首先根据Oracle的官方文档看看CountDownLatch的定义: A synch ...

  2. Java 发送Get和Post请求

    package com.htpt.superviseServices.dm.util; import java.io.BufferedReader; import java.io.IOExceptio ...

  3. C#继承与多态

    继承:在程序中,如果一个类A:类B,这种机制就是继承. 子类可以继承父类的所有内容(成员)吗? 解析: 1.私有成员(属性和方法) 2.构造函数 3.final修饰过的方法,子类不能进行重写 //SE ...

  4. Java网络编程InetAddress类

    InetAddress用来代表IP地址.一个InetAdress的对象就代表着一个IP地址, getByName(String host):在给定主机名的情况下确定主机的 IP 地址,主机名可以是机器 ...

  5. RAutomation 在 Watir中的使用

    RAutomation的用法 require "rautomation" # 通过匹配部分标题来获取窗口 window = RAutomation::Window.new(:tit ...

  6. js格式

    /** * Created by admin on 2017/9/22. */ // 分号后不要再有多余的空格 var name = "North"; var name = &qu ...

  7. hadoop各组件安装(非专业人士,不定期更新)

    压缩包下载http://www.cnblogs.com/bfmq/p/6027202.html 1.zookeepermkdir /usr/local/hadooptar zxf /root/zook ...

  8. hibernate学习四 hibernate关联关系映射

    在Hibernate中对象之间的关联关系表现为数据库中表于表之间的关系(表之间通过外键关联). 1 单向的一对一 主键关联  外键关联 2 单向的一对多 3 单向的多对一 4 单向的多对多 5 双向的 ...

  9. URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)

    URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs) 有时候在IDEA中配置spring文件 ...

  10. SSE2介绍及其简单用法举例

    http://blog.csdn.net/fengbingchun/article/details/18515241