Level:

  Medium

题目描述:

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.

Example 1:

Input:
2
/ \
1 3
Output: true

Example 2:

    5
/ \
1 4
/ \
3 6
Output: false
Explanation: The input is: [5,1,4,null,null,3,6]. The root node's value is 5 but its right child's value is 4.

思路分析:

​  验证一棵二叉树是不是二叉搜索树有两种方法,一种是按照它的性质进行验证,那就是左<根<右,一种是采用中序遍历的方法看它是不是有序的。这里我们采用第一种方法。

代码:

public class Solution{
public boolean isValidBST(TreeNode root){
if(root==null)
return true;
return isBST(root,null,null);
}
public boolean isBST(TreeNode root,Integer low ,Integer high){
if(root==null)
return true;
if(low!=null&&root.val<=low||high!=null&&root.val>=high)
return false;
return isBST(root.left,low,root.val)&&isBST(root.right,root.val,high);
}
}

41.Validate Binary Search Tree(判断是否为二叉搜索树)的更多相关文章

  1. [LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  2. [LeetCode] 270. Closest Binary Search Tree Value 最近的二叉搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  3. 109 Convert Sorted List to Binary Search Tree 有序链表转换二叉搜索树

    给定一个单元链表,元素按升序排序,将其转换为高度平衡的BST.对于这个问题,一个高度平衡的二叉树是指:其中每个节点的两个子树的深度相差不会超过 1 的二叉树.示例:给定的排序链表: [-10, -3, ...

  4. Leetcode109. Convert Sorted List to Binary Search Tree有序链表转换二叉搜索树

    给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10 ...

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

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

  6. leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  7. LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

    题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...

  8. 40.Unique Binary Search Trees(不同的二叉搜索树)

    Level:   Medium 题目描述: Given n, how many structurally unique BST's (binary search trees) that store v ...

  9. Convert Sorted Array to Binary Search Tree转换成平衡二查搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 二分 ...

随机推荐

  1. MySQL的安装及简单调优

    MySQL的安装及调优 1. 安装注意点 ubuntu18的安装方式 sudo apt-get update sudo apt-get install -y mysql-server mysql-cl ...

  2. 【串线篇】Mybatis拓展之MBG

    MBG-逆向工程 一.介绍 MBG:MyBatis Generator:代码生成器: MyBatis官方提供的代码生成器:帮我们逆向生成: 正向: table----javaBean---BookDa ...

  3. 终端、mac等小技巧——2019年10月18日

    1.新建finder窗口 cmd+N 2.查看文件夹结构 brew install tree tree命令行参数(只实用与安装了tree命令行工具): -a 显示所有文件和目录. -A 使用ASNI绘 ...

  4. 浅谈SAP CRM和Hybris Commerce里的价格架构折扣

    最近Jerry做了一个和价格折扣相关的原型项目,把学到的知识记录下来,以备将来查阅. 在这个原型项目里,我们用React-Native开发了一个移动应用,用户可以在手机上浏览SAP Hybris Co ...

  5. 【leetcode】1022. Smallest Integer Divisible by K

    题目如下: Given a positive integer K, you need find the smallest positive integer N such that N is divis ...

  6. Sql server 2008 的完成备份和差异备份还原

    当数据库数据量不大的情况下用 Sqlserver 的完全备份就完全可以了 步骤为: 1.在需要还原的数据库上右键选择如图 2.在“常规”选项中点击“源设备”选取磁盘上备份好的.bak文件后,勾上“还原 ...

  7. 【CF1210C】Kamil and Making a Stream(vector,数论,树)

    题意:给定一棵n个点带点权的树,i号点的点定义f(i,j)为i到j路径上所有点的gcd,其中i是j的一个祖先,求所有f(i,j)之和mod1e9+7 2<=n<=1e5,0<=a[i ...

  8. Alex and Number

    Alex and Number 时间限制: 1 Sec  内存限制: 128 MB提交: 69  解决: 12[提交][状态] 题目描述 Alex love Number theory. Today ...

  9. jerry

    jerry 题目描述 众所周知,Jerry 鼠是一只非常聪明的老鼠. Jerry 聪明到它可以计算64 位有符号整形数字的加减法. 现在,Jerry 写下了一个只由非负整数和加减号组成的算式.它想给这 ...

  10. Gym-100676E Time Limit Exceeded?

    原题链接 https://odzkskevi.qnssl.com/1110bec98ca57b5ce6aec79b210d2849?v=1491063604 ********************* ...