给定一个二叉树,判断其是否是一个有效的二叉搜索树。
一个二叉搜索树有如下定义:
    左子树只包含小于当前节点的数。
    右子树只包含大于当前节点的数。
    所有子树自身必须也是二叉搜索树。
示例 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. 分布式session之token解决方案实现

    基于令牌(Token)方式实现Session解决方案,因为Session本身就是分布式共享连接 用token代替session 废话不多说,看项目: pom.xml <project xmlns ...

  2. css元素定位样式

    曾经写网页,学css整体上不难,但就是元素定位,始终一知半解,直到今天,本着实践出真知的理念,经过认真测试,总结出了如下结论. css 定位: positionstatic : 默认静止定位,元素在正 ...

  3. php封装数据库mysql, mysqli

    <?php header("content-type:text/html;charset=utf-8"); class db{    //私有的静态属性    private ...

  4. CodeForces768B:Code For 1 (分治)

    Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On hi ...

  5. cogs1070玻璃球游戏

    1070. [焦作一中2012] 玻璃球游戏 ★   输入文件:marbles.in   输出文件:marbles.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述] 小x ...

  6. python3 分布式进程(跨机器)BaseManager(multiprocessing.managers)

    A机器负责发送任务和接受结果: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #ta ...

  7. Win32环境下代码注入与API钩子的实现

    本文详细的介绍了在Visual Studio(以下简称VS)下实现API钩子的编程方法,阅读本文需要基础:有操作系统的基本知识(进程管理,内存管理),会在VS下编写和调试Win32应用程序和动态链接库 ...

  8. poj2226Muddy Fields——二分图匹配

    题目:http://poj.org/problem?id=2226 把行连通块作为左部点,列连通块作为右部点,行列连通块有相交的格子就连边: 则问题转化为求最小点覆盖,即最大匹配. 代码如下: #in ...

  9. JAVA 内部类 (三)实例

    为什么要用内部类:控制框架 一个“应用程序框架”是指一个或一系列类,它们专门设计用来解决特定类型的问题.为应用应用程序框架,我们可从一个或多个类继承,并覆盖其中的部分方法.我们在覆盖方法中编写的代码用 ...

  10. selenium_page_object

    最简单的pageobject github地址:https://github.com/defnngj/selenium_page_objects