判断二叉搜索树的方法是:

中序遍历形成递增序列

//全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量
List<Integer> list = new ArrayList<>();
public boolean isValidBST(TreeNode root) {
/*
这里要注意二叉搜索树的左子树的每个节点都要小于根节点,不只是左孩子,右子树也同样,
常犯的一个错误就是只是判断了左孩子和右孩子
正确方法是:中序遍历法,中序遍历之后的结果是一个递增序列
判断是否是二叉搜索树的方法是:中序遍历法,记住
*/
if (root==null) return true;
inOrder(root);
for (int i = 1; i < list.size(); i++) {
if (list.get(i)<=list.get(i-1))
return false;
}
return true;
}
void inOrder(TreeNode root){
if (root==null)
return;
inOrder(root.left);
list.add(root.val);
inOrder(root.right);
}

[LeetCode98]98. Validate Binary Search Tree判断二叉搜索树的更多相关文章

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

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

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

  4. Leetcode98. Validate Binary Search Tree验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...

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

  6. 098 Validate Binary Search Tree 验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树.一个二叉搜索树有如下定义:    左子树只包含小于当前节点的数.    右子树只包含大于当前节点的数.    所有子树自身必须也是二叉搜索树.示例 1 ...

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

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

  9. Validate Binary Search Tree——体现二查搜索树思想的一道题

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. passwrod和shadow文件介绍

    1./etc/passwd #cat /etc/passwdroot:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbi ...

  2. @RequestParam,@RequestBody,@PathVariable注解还分不清吗?

    前言 在使用 SpringMVC 开发时,经常遇到前端传递的各种参数,比如 form 表单,JSON 数据,String[] 数组,再或者是最常见的 String 字符串等等,总之大部分场景都是在标题 ...

  3. Python之【模块】

    双层装饰器 一个函数可以被多个装饰器装饰: 多层装饰器的本质是:嵌套: 执行规则是:解释自下而上,执行自上而下 •简单的用户权限验证程序: USE_INFO = {} # 初始化一个字典,用户存放用户 ...

  4. python实现自动发邮件

    Python有两个内置库:smtplib和email,可以实现邮件功能,无需下载,直接import导入. smtplib库负责发送邮件 Email库负责构造邮件格式和内容 邮件发送需要遵守SMTP协议 ...

  5. 题解-FJOI2018 领导集团问题

    题面 FJOI2018 领导集团问题 给一棵树 \(T(|T|=n)\),每个点有个权值 \(w_i\),从中选出一个子点集 \(P=\{x\in {\rm node}|x\in T\}\),使得 \ ...

  6. 【学习笔记】最小直径生成树(MDST)

    简介 无向图中某一点(可以在顶点上或边上),这个点到所有点的最短距离的最大值最小,那么这个点就是 图的绝对中心. 无向图所有生成树中,直径最小的一个,被称为 最小直径生成树. 图的绝对中心的求法 下文 ...

  7. 题解-CF1065E Side Transmutations

    CF1065E Side Transmutations \(n\) 和 \(m\) 和 \(k\) 和序列 \(b_i(1\le i\le m,1\le b_i\le b_{i+1}\le \frac ...

  8. 通过git-bash 批量管理VMware虚拟机

    #先将vmrun .exe 加入环境变量 # 我这里是: ;C:\Program Files (x86)\VMware\VMware VIX; #cd E:/期中架构/#sh new\ 3.bash ...

  9. STL——容器(deque) deque 的大小

    1. deque 的大小 deque.size();              //返回容器中元素的个数 1 #include <iostream> 2 #include <dequ ...

  10. writeset参数配置探索——究竟在哪个角色上配置参数?

    关于writeset,一直以来我都是所有节点同时配置下面参数: binlog_transaction_dependency_tracking=WRITESET transaction_write_se ...