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

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public boolean isValidBST(TreeNode root) {
  12. if(root==null){
  13. return true;
  14. }
  15. return isValidBST(root,Long.MIN_VALUE, Long.MAX_VALUE);
  16. }
  17. private boolean isValidBST(TreeNode root,long min,long max){
  18. if(root==null){
  19. return true;
  20. }else if(root.val<=min||root.val>=max){
  21. return false;
  22. }
  23. return isValidBST(root.left,min,root.val)&&isValidBST(root.right,root.val,max);
  24. }
  25. }

参考: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. 人生苦短之Python列表拷贝

    列表拷贝的几种方法:    1.工厂函数 b=list(a) >>> a=[1,2,3,4] >>> b=list(a) >>> b [1, 2, ...

  2. 关于AngularJs中监听事件及脏循环的理解

    可能很多刚入行或者刚学习的前端对于AngularJs中的一些事件或者概念感觉不理解或者没有思路,今天让我们一起来剖析一下AngularJs中的一些事件. AngularJs中对于的监听事件会用到一个s ...

  3. 阿里云数据库产品HybridDB简介——OLAP数据库,支持行列混合存储,基于数据库Greenplum的开源版本,并且吸收PostgreSQL精髓

    为什么会有HybridDB的诞生?它经历了怎样的研发历程?它的应用场景和情况是怎样的?带着这些问题,InfoQ对阿里云的数据库专家兼Postgres中国社区/中国用户会主席萧少聪先生进行了采访,以下文 ...

  4. cassandra压缩——从文档看,本质上也应该是在做块压缩

    Compression Compression maximizes the storage capacity of Cassandra nodes by reducing the volume of ...

  5. HDU3666 THE MATRIX PROBLEM (差分约束+取对数去系数)(对退出情况存疑)

    You have been given a matrix C N*M, each element E of C N*M is positive and no more than 1000, The p ...

  6. java 02 内部类

  7. SimpliciTI Sample Applications

    Sample Applications 介绍了4个简单的示例应用程序来演示SimpliciTI的各种特性和功能. Simple Peer-To-Peer :two linked End-Devices ...

  8. redis 操作使用

    /*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host,端口为6379,超 ...

  9. Eigen中的noalias(): 解决矩阵运算的混淆问题

    作者:@houkai本文为作者原创,转载请注明出处:http://www.cnblogs.com/houkai/p/6349990.html 目录 混淆例子解决混淆问题混淆和component级的操作 ...

  10. c++的const总结(转)

    为什么使用const?采用符号常量写出的代码更容易维护:指针常常是边读边移动,而不是边写边移动:许多函数参数是只读不写的.const最常见用途是作为数组的界和switch分情况标号(也可以用枚举符代替 ...