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. shell函数的结束与返回值

  2. Linux就该这么学09学习笔记

    参考链接:https://www.linuxprobe.com/chapter-09.html 配置网卡服务 配置网卡参数 1.执行nmtui命令运行网络配置工具 2.选中Edit a connect ...

  3. 初学Java 九九乘法表

    public class MultiplicationTable { public static void main(String[] args) { System.out.println(" ...

  4. poj2689 Prime Distance(素数区间筛法)

    题目链接:http://poj.org/problem?id=2689 题目大意:输入两个数L和U(1<=L<U<=2 147 483 647),要找出两个相邻素数C1和C2(L&l ...

  5. ubuntu安装deb包(dpkg)

    安装 sudo dpkg -i DEB_PACKAGE 卸载 sudo dpkg -r PACKAGE_NAME 重新配置已安装的包 Reconfigure an existing package 例 ...

  6. js手机号码中间用星号代替。银行卡后四位之外改为星号

    如手机号码13123456789,中间四位用'*'代替 var phone='13123456789' 方法1(字符串的截取): ,)+); 方法2(正则表达式): })\d{}(\d+)/,&quo ...

  7. vue Echarts自适应浏览器窗口大小

    <template> <div class="chinaecharts"> <div id="mapChart" ref=&quo ...

  8. VMware linux 克隆机的配置

    从另一台虚拟机克隆完后的一些配置 编辑eth0的配置文件: [root@wen data01:4]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 删除 ...

  9. delphi 创建桌面快捷方式

    unit UShorCut; interface uses Windows, Classes, SysUtils,Dialogs, ShlObj, ComObj, ActiveX, Registry; ...

  10. window安装nodejs

    nvm管理nodejs 原文: https://www.cnblogs.com/shimily/articles/7244058.html1.下载nvm(nodejs版本管理工具) https://g ...