验证二叉查找树 · 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) ...
随机推荐
- Windows7无法访问共享文件夹(0x800704cf,0x80070035)解决方法
Windows7系统突然无法访问Linux的samba服务器,出现0x800704cf或者0x80070035错误,也不能访问其他windows机器的共享文件夹,解决方案如下两张图,配置与下面两张图为 ...
- 原生态的javascript的n种技巧(我从别人的博客中拷贝过来的,方便以后查阅)
1.原生JavaScript实现字符串长度截取 function cutstr(str, len) { var temp; var icount = 0; var patrn = /[^\x00-\x ...
- ExtJS模板与菜单的使用案例-床位卡
ExtJS的模板的使用: 项目中场景基本就是表格模型: TPL:自己编写模板 store:数据源 UI组件: tbar,rbr,bbar实现工具栏 PageBar与StatusBar:可以针对TPL的 ...
- AFNetworkingErrorDomain 错误解决方法
首先我们来看一下错误信息: Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299 ...
- 腾讯优图&港科大提出一种基于深度学习的非光流 HDR 成像方法
目前最好的高动态范围(HDR)成像方法通常是先利用光流将输入图像对齐,随后再合成 HDR 图像.然而由于输入图像存在遮挡和较大运动,这种方法生成的图像仍然有很多缺陷.最近,腾讯优图和香港科技大学的研究 ...
- Tornado之模板
知识点 静态文件配置 static_path StaticFileHandler 模板使用 变量与表达式 控制语句 函数 块 4.1 静态文件 现在有一个预先写好的静态页面文件 (下载静态文件资源), ...
- 位运算骚操作 Part 1
▶ 原文标题<Bit Twiddling Hacks>,地址:https://graphics.stanford.edu/~seander/bithacks.html ▶ 额外参考资料:h ...
- oracle使用随笔
一,centos 7安装桌面环境 首先使用命令yum grouplist查看可以使用的group ,第一步,命令安装Gnome Desktop,第二步,yum groupinstall "X ...
- sqlserver主从复制
参考网站: http://www.178linux.com/9079 https://www.cnblogs.com/tatsuya/p/5025583.html windows系统环境进行主从复制操 ...
- leetcode242
public class Solution { public bool IsAnagram(string s, string t) { Dictionary<char, int> dic ...