算法分析:两种方法,一种是中序遍历,然后得到一个序列,看序列是否是有序的。第二种,是用递归。

中序遍历:

public class Solution {
List<Integer> list = new ArrayList<>();
public boolean isValidBST(TreeNode root) {
if(root == null)
{
return true;
}
inorderTraversal(root);
for(int i = 0; i < list.size() - 1; i ++)
{
if(list.get(i) >= list.get(i+1))
{
return false;
}
}
return true;
}
public void inorderTraversal(TreeNode root)
{
if(root == null)
{
return ;
}
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
}
}

递归:

public boolean isValidBST3(TreeNode root) {
return isValidBST(root, null, null);
}
public boolean isValidBST(TreeNode root, Integer min, Integer max)
{
if(root == null)
{
return true;
}
if(min != null && root.val <= min)
{
return false;
}
if(max != null && root.val >= max)
{
return false;
}
return isValidBST(root.left, min, root.val) && isValidBST(root.right, root.val, max);
}

Validate Binary Search Tree,判断是否是二叉排序树的更多相关文章

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

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

  2. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  3. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  4. Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  5. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  6. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  7. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  8. LintCode Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  9. 39. Recover Binary Search Tree && Validate Binary Search Tree

    Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...

  10. [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 ...

随机推荐

  1. 【BZOJ3677】[Apio2014]连珠线 换根DP

    [BZOJ3677][Apio2014]连珠线 Description 在列奥纳多·达·芬奇时期,有一个流行的童年游戏,叫做“连珠线”.不出所料,玩这个游戏只需要珠子和线,珠子从1到礼编号,线分为红色 ...

  2. 【BZOJ1692】[Usaco2007 Dec]队列变换 后缀数组+贪心

    [BZOJ1692][Usaco2007 Dec]队列变换 Description FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比 ...

  3. Android 仿微信朋友圈发动态功能(相册图片多选)

    代码分享 代码名称: 仿微信朋友圈发动态功能(相册图片多选) 代码描述: 仿微信朋友圈发动态功能(相册图片多选) 代码托管地址: http://www.apkbus.com/android-15276 ...

  4. sqlserver 2012隐藏查询结果窗口

    1, 08r2的时候都是可以用 ctrl+r隐藏的 到了2012就不行了 方法 . 2, 快捷键的设置

  5. Redis连接(二)

    Redis 命令 Redis 命令用于在 redis 服务上执行操作. 要在 redis 服务上执行命令需要一个 redis 客户端.Redis 客户端在我们之前下载的的 redis 的安装包中. 语 ...

  6. Yii数据库子查询嵌入select中,而不是where条件语句中

    $subQuery = (new Query())->select('COUNT(*)')->from('user'); // SELECT `id`, (SELECT COUNT(*) ...

  7. 洛谷P2444 病毒【AC自动机】

    题目描述 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码是安全的.现在委员会已经找出了所有的病毒代码段,试问,是否 ...

  8. Oracle Schema Objects——PARTITION

    Oracle Schema Objects 表分区 表- - 分区( partition )TABLE PARTITION 一段时间给出一个分区,这样方便数据的管理. 可以按照范围range分区,列表 ...

  9. dp\dpi\px\pt\em单位长度理解

    屏幕都有固定的物理长宽度属性和分辨率 比如电脑.比如手机屏幕 例如有手机屏幕尺寸是1.5英寸x2英寸,屏幕分辨率为240x320, 那么可以推算水平方向每英寸的像素数(dpi)是:240/1.5=16 ...

  10. CSS伪代码

    /*在p之后插入我是好人*/ p.first:after { content: "好人" } /*在p之前插入亲爱的朋友men*/ p:before { content: &quo ...