[Leetcode 98]判断有效的二叉搜索树Validate Binary Search Tree
题目
https://leetcode.com/problems/validate-binary-search-tree
判断所给二叉树是否是二叉搜索树
二叉搜索树:其值left<root<right
*[2,2,2]这种相等的情况也不是搜索树
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
A valid BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Example 1:

Input: root = [2,1,3]
Output: true
Example 2:

Input: root = [5,1,4,null,null,3,6]
Output: false
Explanation: The root node's value is 5 but its right child's value is 4.
思路
参考:https://www.bilibili.com/video/BV1VK411N7vm
首先最直观的方法,从左到右值越来越大
想到使用中序遍历(左中右),将所有元素提取出来成list
如果是搜索树,则list里的元素值依次递增
再对list遍历,如果list[i+1]<list[i] 则返回false结束
遍历完成,返回true
法二
上面的思路遍历了两次,时间空间复杂度↑ 优化,在遍历时就开始判断
以root对半分
root左取值范围【负无穷,root值】
root右取值返回【root值,正无穷】
*注意因为这里int类型,题目说int_min/max都能取到,所有我们tmp出的初始正负无穷要用long,更大
代码
法一
https://www.cnblogs.com/inku/p/15163602.html
class Solution {
List<Integer> list=new ArrayList<>();
public void inorder(TreeNode root){
if (root==null)
return;
inorder(root.left);
list.add(root.val);
inorder(root.right);
}
public boolean isValidBST(TreeNode root) {
if(root==null)
return true;
inorder(root);
int min=list.get(0);
for(int i=1;i<list.size();i++){
if (min>=list.get(i)){
return false; //严格小于,等于也是false
}
else{
min=list.get(i);
}
}
return true;
}
}
法二
class Solution {
public boolean isValidBST(TreeNode root) {
return fun(root,Long.MIN_VALUE,Long.MAX_VALUE);//初始化最大最小值
}
public boolean fun(TreeNode root,Long min,Long max){
if (root==null)
return true;
boolean left=fun(root.left,min,(long)root.val);//root左边,最大不超过root,值越来越小
//反之,返回false
if(root.val<=min||root.val>=max)
return false;
boolean right=fun(root.right,(long)root.val,max);//root右边,最小不小于root,值越来越大
return left&&right;//二者中一个false,就为false
}
}
[Leetcode 98]判断有效的二叉搜索树Validate Binary Search Tree的更多相关文章
- [Swift]LeetCode98. 验证二叉搜索树 | Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- 二叉搜索树BST(Binary Search Tree)
二叉搜索树(Binary Search Tree)也叫二叉排序树或二叉查找树.它满足以下性质: 1.非空左子树的所有键值小于其根结点的键值: 2.非空右子树的所有键值大于其根结点的键值: 3.左右子树 ...
- [Swift]LeetCode173. 二叉搜索树迭代器 | Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- 数据结构-二叉搜索树(BST binary search tree)
本文由@呆代待殆原创,转载请注明出处:http://www.cnblogs.com/coffeeSS/ 二叉搜索树简介 顾名思义,二叉搜索树是以一棵二叉树来组织的,这样的一棵树可以用一个链表数据结构来 ...
- 原生JS实现二叉搜索树(Binary Search Tree)
1.简述 二叉搜索树树(Binary Search Tree),它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子 ...
- 【数据结构05】红-黑树基础----二叉搜索树(Binary Search Tree)
目录 1.二分法引言 2.二叉搜索树定义 3.二叉搜索树的CRUD 4.二叉搜索树的两种极端情况 5.二叉搜索树总结 前言 在[算法04]树与二叉树中,已经介绍过了关于树的一些基本概念以及二叉树的前中 ...
- 二叉搜索树(Binary Search Tree)
二叉搜索树(BST,Binary Search Tree),也称二叉排序树或二叉查找树. 二叉搜索树:一棵二叉树,可以为空:如果不为空,满足以下性质: 非空左子树的所有键值小于其根结点的键值: 非空右 ...
- 二叉搜索树(Binary Search Tree)--C语言描述(转)
图解二叉搜索树概念 二叉树呢,其实就是链表的一个二维形式,而二叉搜索树,就是一种特殊的二叉树,这种二叉树有个特点:对任意节点而言,左孩子(当然了,存在的话)的值总是小于本身,而右孩子(存在的话)的值总 ...
- [Swift]LeetCode99. 恢复二叉搜索树 | Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
随机推荐
- 家里静态Ip设置
分配的网段是10.10.1.*
- Android Custom View使用Databinding
Android Custom View是可以使用 databinding 的 //java.lang.IllegalArgumentException: View is not a binding l ...
- ISTQB软件测试初级认证模拟题
参考地址 http://www.docin.com/p-297467364.html 第一章:软件测试基础(18%) 1.学习目标 1.1 为什么需要软件测试? (K2) ① 通过具体的例子,来描述软 ...
- HDLbits——Mt2015 lfsr
1.描述电路图里面的一个子模块 Assume that you want to implement hierarchical Verilog code for this circuit, using ...
- SAP 内外交货单过账
* 交货单过账 DATA: LS_HEADER_DATA TYPE BAPIIBDLVHDRCON, LS_HEADER_CONTROL TYPE ...
- TODOList小黄条
TODOList http://www.yynote.cn/ 总结 windows中的神器
- kibana7.6.2内网windows系统下编译打包部署
1.在kibana根目录下执行命令: yarn build --skip-os-packages 2.报错无法下载node:将node相关文件下载放到kibana/.node_binaries/10 ...
- window批处理一键打开多个exe
使用批处理的start命令,格式为start /d "绝对路径" 目标exe名,记得路径和exe名间有个空格 @echo off start /d "E:\demo\&q ...
- crontab执行不生效
背景:不知道什么原因脚本手动执行:正常:crontab执行不生效: 1.将命令所属路径加入到 /etc/crontab中, 2.在shell脚本中加入 source /etc/profile
- DB2日常维护操作
一. DB2日常维护操作 1.数据库的启动.停止.激活 db2 list active databases db2 active db 数据库名 db2start --启动 db2stop [forc ...