Validate Binary Search Tree,判断是否是二叉排序树
算法分析:两种方法,一种是中序遍历,然后得到一个序列,看序列是否是有序的。第二种,是用递归。
中序遍历:
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,判断是否是二叉排序树的更多相关文章
- [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树
判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...
- 【leetcode】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【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) ...
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 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 ...
- [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 ...
随机推荐
- java - OutOfMemoryError: Java heap space 堆空间不足
Error occurred during initialization of VM Could not reserve enough space for object heap Error: Cou ...
- Egret Wing4.0.3 动画编辑器
一 exml上摆放组件 切换动画编辑 创建动画组,命名test1. 选中一个对象,创建动画(必须选中一个对象后,+号才会亮.且一个对象只能创建一个动画) 之后和Flash差不多.在时间轴插入关键帧. ...
- 【BZOJ3437】小P的牧场 斜率优化
[BZOJ3437]小P的牧场 Description 背景 小P是个特么喜欢玩MC的孩纸... 描述 小P在MC里有n个牧场,自西向东呈一字形排列(自西向东用1…n编号),于是他就烦恼了:为了控制这 ...
- mysql如何查询当前周的第一天的日期?
转自:http://blog.csdn.net/zzhongcy/article/details/43016685 select date_sub(curdate(),INTERVAL WEEKDAY ...
- python-social-auth with Django: ImportError: No module named 'social_django' 解决方法
To use Django with python social auth, you need to install the Django app as well. You can specify t ...
- 委托(Func与Action)
1.平时我们如果要用到委托一般都是先声明一个委托类型,比如: private delegate string Say(); string说明适用于这个委托的方法的返回类型是string类型,委托名Sa ...
- 牛客网多校赛第七场J--Sudoku Subrectangle
链接:https://www.nowcoder.com/acm/contest/145/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6553 ...
- IIS中User-mode caching引起的Cache-Control不为public的问题
在IIS的Output caching中如果启用了User-mode caching将引起Cache-Control为no-cache,从而造成页面不能被浏览器或代理服务器缓存. web.config ...
- python问答模块
""" 该模块功能:获取用户的输入文本,通过输入文本和数据库中的关键主题文本相比较, 获取最佳的回答内容 """ import xlrd i ...
- mysql 数据操作 单表查询 group by 聚合函数
强调: 如果我们用unique的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义 多条记录之间的某个字段值相同,该字段通常用来作为分组的依据 如果按照每个字段都是唯一的进行分组,意味着按照这 ...