098 Validate Binary Search Tree 验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树。
一个二叉搜索树有如下定义:
左子树只包含小于当前节点的数。
右子树只包含大于当前节点的数。
所有子树自身必须也是二叉搜索树。
示例 1:
2
/ \
1 3
二叉树[2,1,3], 返回 true.
示例 2:
1
/ \
2 3
二叉树 [1,2,3], 返回 false.
详见:https://leetcode.com/problems/validate-binary-search-tree/description/
Java实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
if(root==null){
return true;
}
return isValidBST(root,Long.MIN_VALUE, Long.MAX_VALUE);
}
private boolean isValidBST(TreeNode root,long min,long max){
if(root==null){
return true;
}else if(root.val<=min||root.val>=max){
return false;
}
return isValidBST(root.left,min,root.val)&&isValidBST(root.right,root.val,max);
}
}
参考:https://www.cnblogs.com/grandyang/p/4298435.html
098 Validate Binary Search Tree 验证二叉搜索树的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- [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 ...
- [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 ...
- [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树
判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- 人生苦短之Python的urllib urllib2 requests
在Python中涉及到URL请求相关的操作涉及到模块有urllib,urllib2,requests,其中urllib和urllib2是Python自带的HTTP访问标准库,requsets是第三方库 ...
- python学习笔记:第二天(基本数据类型)
Python3 基本数据类型 1.标准数据类型 Python3中有六个标准的数据类型:Number(数字).String(字符串).List(列表).Tuple(元组).Sets(集合).Dictio ...
- Linux档案属性
输入命令:ls -al 档案类型权限: 第一個字元代表这个档案是『目录.档案或链接档等等』: 当为[ d ]则是目录: 当为[ - ]则是目录: 若是[ l ]则表示为链接档(link file): ...
- php的CURL使用及例子
使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了.无论是你想从从一个链接上取部分数据,或是取一个XML文件并把 ...
- Material Design 之 定义状态栏(Status Bar)的颜色
Hey,好久不见.今天遇到一个问题,想要把Status Bar 和 Tool Bar的颜色弄成一样的,或者是类似的,例如Material Design: 图中Status Bar颜色比Tool Bar ...
- IntelliJ IDEA 2018 设置代码提示对大小写不敏感
setting->Editor->General->Code Completion取消勾选Match case
- JVM介绍(一)
JVM是运行在操作系统之上的,它与硬件没有直接的交互 类装载器ClassLoader 负责加载class文件,class文件在文件开头有特定的文件标示,并且ClassLoader只负责class文件的 ...
- vue render函数使用jsx语法 可以使用v-model语法 vuex实现数据持久化
render函数使用jsx语法: 安装插件 transform-vue-jsx 可以使用v-model语法安装插件 jsx-v-model .babelrc文件配置: vuex实现数据持久化 安装插 ...
- World is Exploding
题意: 给出一个长为n的序列A,问有多少四元组(a, b, c, d)满足$a \ne b \ne c \ne d, 1 \leq a < b \leq n, 1 \leq c < d \ ...
- intellj idea 使用
1. 导入包快捷 Alt + Enter 2. 查看方法注释,点击进入源码即可,若想和eclipse一样鼠标停留即可出现注释提示,开启方法为: Preferences->Editor->G ...