验证二叉查找树 · Validate Binary Search Tree
[抄题]:
[思维问题]:
不知道要定义resultType, 其实用仔细分析判断条件就行了:是否是bst+最大最小值
类似于平衡二叉树:是否平衡+左右的高度差
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
1-1也不是。所以left.max >= root.val也不行
[画图]:
[一刷]:
- 空节点可以认为是平衡二叉树
- 只有在root.left非空,并且直接拿left.max比root的取val大时,才能否定
- 最后可以直接返回left.min, right.max
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[总结]:
定义结构-helper方法-(边界条件-不符合的情况-符合的情况)-调用helper方法
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构,为什么不用别的数据结构]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
BST中出现最多的元素:中序遍历,然后计数统计
public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if the binary tree is BST, or false
*/
public boolean isValidBST(TreeNode root) {
ResultType r = validateHelper(root);
return r.is_bst;
}
private ResultType validateHelper(TreeNode root) {
if (root == null) {
return new ResultType(true, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
ResultType left = validateHelper(root.left);
ResultType right = validateHelper(root.right);
if (!left.is_bst || !right.is_bst) {
// if is_bst is false then minValue and maxValue are useless
return new ResultType(false, 0, 0);
}
if (root.left != null && left.maxValue >= root.val ||
root.right != null && right.minValue <= root.val) {
return new ResultType(false, 0, 0);
}
return new ResultType(true,
Math.max(root.val, right.maxValue),
Math.min(root.val, left.minValue));
}
}
也可以不定义resulttype 不用这种奇葩思路。直接在isvalidate函数中定义节点为空或者大小不对就行了

验证二叉查找树 · 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 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- 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) ...
- 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 ...
- 【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) ...
随机推荐
- oletools下载安装及rtfobj使用
rtf内嵌对象分析提取工具rtfobj是oletools的一部分 oletools各个版本下载地址https://bitbucket.org/decalage/oletools/downloads/ ...
- 基于WMI获取本机真实网卡物理地址和IP地址
using System; using System.Collections.Generic; using System.Management; using System.Runtime.Intero ...
- C++多线程同步之Semaphore(信号量)
一.线程间同步的几种方式 从上篇博文中可以发现,当多个线程对同一资源进行使用时,会产生“争夺”的情况,为了避免这种情况的产生,也就出现了线程间的同步这个技术.线程间的同步有多种方式,在接下来的博文中我 ...
- 中国Linux开源镜像站大全
本文来源:各大开源软件.发行版镜像页面. 请注意这是一个总结,如果您自己搭建了一个小型开源镜像,这里并没有.以下列出的是包含大量不同镜像的站点. 具体配置中,我建议您使用大企业 ...
- sklearn的estimator
estimator的工作流程 在sklearn中,估计器(estimator)是一个重要的角色,分类器和回归器都属于estimator.在估计器中有有两个重要的方法是fit和transform. fi ...
- J2SE 8的输入输出--序列化
1. 普通序列化 implements Serializable 继承Serializable接口 class Employee implements Serializable { private S ...
- Mysql 表名大写 找不到表
原来Linux下的MySQL默认是区分表名大小写的,通过如下设置,可以让MySQL不区分表名大小写:1.用root登录,修改 /etc/my.cnf:2.在[mysqld]节点下,加入一行: lowe ...
- 多线程--Java
多线程: 1.进程和线程 进程是资源分配的最小单位,线程是CPU调度的最小单位. 每个进程的创建都需要系统为其开辟资源内存空间,并发执行的程序在执行过程中分配和管理资源的基本单位,速度和销毁也较慢.进 ...
- opencv一些资料
中文论坛: http://www.opencv.org.cn/forum.php?mod=forumdisplay&fid=1 yuv与mat转换: https://www.cnblogs.c ...
- http get 方式参数的长度限制<转>
http get 方式参数的长度限制 这个问题一直以来似乎是被N多人误解,其实Http Get方法提交的数据大小长度并没有限制,而是IE浏览器本身对地址栏URL长度有最大长度限制:2048个字符. 当 ...