验证二叉查找树 · 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) ...
随机推荐
- Linux防火墙(Firewalls)
结构关系图 查看这两个防火墙文件 # cat /etc/hosts.deny # cat /etc/hosts.allow 查看Linux中防火墙的状态 某个服务是否能由tcpwraps来进行控制关键 ...
- [UE4]GameMode
GameMode定义了正在玩的游戏规则,积分等方面,游戏中有些数据和逻辑不适合放在某一个对象身上,这些数据在整个游戏运行中腰持续存在的(比如:积分.排名). 每次游戏一启动,GameMode就被创建, ...
- crs_stop 错误一列
http://www.forzw.com/archives/703 grid 与 oracle 版本为11.2.0.4,为两节点RAC,在通过crs_stop -all命令关闭oracle服务时出 ...
- GSO/TSO/GRO等对VirtIO虚机的网络性能影响分析(by quqi99)
作者:张华 发表于:2016-04-05版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 ( http://blog.csdn.net/quqi99 ) IP层 ...
- 【求助】win 2008 R2 远程桌面多用户,破解最大连接数2的限制
[求助]win 2008 R2 远程桌面多用户,破解最大连接数2的限制. 1. 本地组策略设置的是“允许的RD最大连接数 5”. 2. 远程桌面仍然只能有两个连接在线. 3. 后来发现是下面这个设置限 ...
- PyQt5系列教程
PyQt5系列教程(一)Mac OS X下搭建Python3.5.1+PyQt5开发环境PyQt5系列教程(二)利用QtDesigner设计UI界面PyQt5系列教程(三)用py2exe进行程序打包P ...
- svn快速安装
windows版本控制系统. 用VisualSVN server 服务端和 TortoiseSVN客户端搭配使用 软件下载 VisualSVN-Server:http://subversion.apa ...
- TestNG中DataProvider的用法
提供数据的一个测试方法.注解的方法必须返回一个Object[] [],其中每个对象 []的测试方法的参数列表中可以分配.该@Test 方法,希望从这个 DataProvider 的接收数据,需要使用一 ...
- viewer 照片查看器
viewer 照片查看器 效果: api: https://github.com/fengyuanchen/viewerjs#methods npm: npm install viewerjs 使用: ...
- idea中spring boot启动后无法访问jsp
出自:https://www.jianshu.com/p/470b28d76147 第一种: 打开File > Project Structure > Facetes 如图1: 图1 如果 ...