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 ...
随机推荐
- shell中特殊变量$0 $1 $# $$ $! $?的涵义
$0: 执行脚本的名字 $*和$@: 将所有参数返回 $#: 参数的个数 $_: 代表上一个命令的最后一个参数 $$: 代表所在命令的PID $!: 代表最后执行的后台命令的PID $?: 代表上一个 ...
- 微信小程序5.2.2版本,找不着resource下exml皮肤
问题描述: egret engine 5.2.2 原来5.1.11好好的,一升级就跪了 新建一个项目,找不到皮肤... 已发到论坛问去了,现在只能手动复制皮肤到小游戏目录下... 解决方案: 卸载重新 ...
- js调试模式怎么看变量是在哪里定义的?
1. 2.
- 最小树形图(poj3164)
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 12834 Accepted: 3718 ...
- 通过JS模拟select表单,达到美化效果[demo]
.m-form{background:#fff;padding:50px;font-family:12px/1.5 arial,\5b8b\4f53,sans-serif;} .m-form ul,. ...
- nginx软件的编译安装步骤
1.1 检查软件安装的系统环境 [root@web02 conf]# cat /etc/redhat-release CentOS release 6.8 (Final) [root@web02 co ...
- Python大数据:jieba 中文分词,词频统计
# -*- coding: UTF-8 -*- import sys import numpy as np import pandas as pd import jieba import jieba. ...
- 精通linux设备驱动开发 笔记
3.2.7 错误处理 #include <linux/err.h> char * collect_data(char *userbuffer) { char *buffer; /* ...
- python调用API
相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍Python中调用API的几种方式,下面是 ...
- CentOS 6.4下Squid代理服务器的安装与配置(转)
add by zhj: 其实我们主要还是关注它在服务器端使用时,充当反向代理和静态数据缓存.至于普通代理和透明代理,其实相当于客户端做的事,和服务端没有什么关系.另外,Squid的缓存主要是缓存在硬盘 ...