[leetcode]_Validate Binary Search Tree
题目:判断一棵二叉树是否合法。要求二叉树满足 左子树所有值 < 当前值 < 右子树所有值,并且所有点都满足这个条件。
思路:
1、从当前根节点判断,求根节点左子树最大值maxLeft,右子树最小值minRight。
2、判断当前节点值是否满足 maxLeft < current < minRight。
3、如果满足,则递归地判断其左右子树是否同样合法。
代码:
1 public boolean isValidBST(TreeNode root) {
if(root == null) return true;
int maxLeft = maxValue(root.left);
int minRight = minValue(root.right);
return maxLeft < root.val && minRight > root.val && isValidBST(root.left) && isValidBST(root.right);
}
public int maxValue(TreeNode node){
if(node == null) return Integer.MIN_VALUE;
int leftMax = maxValue(node.left);
int rightMax = maxValue(node.right);
return Math.max(node.val , Math.max(leftMax , rightMax));
}
public int minValue(TreeNode node){
if(node == null) return Integer.MAX_VALUE;
int leftMin = minValue(node.left);
int rightMin = minValue(node.right);
return Math.min(node.val , Math.min(leftMin , rightMin));
}
网络上还有一种更简单的解法。今天好累啊,明天再做了。see you~
[leetcode]_Validate Binary Search Tree的更多相关文章
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [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 Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- LeetCode Closest Binary Search Tree Value
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
- [leetcode]Recover Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...
随机推荐
- UVALive 6915 J - Leveling Ground
思路: 简单模拟下.从左向右扫描一次,求出挖出该区间空地的花费,并取个最小值即可. 至于怎么求区间内的高度最小值,就用线段树就好了. #include <bits/stdc++.h> #d ...
- C++中char类型的十六进制字符串转换成字节流
如a[5]="1234"转换成a[5]={0x12,0x34} 代码如下: void HexStrToByte(const char* source, unsigned char* ...
- Linux静默安装Oracle
打算在云服务器上装oracle服务,以前DBA美眉都是在图形化界面下安装,这次抓瞎了.赶紧上网查查,静默安装可以解决问题.于是乎赶紧开始部署,过程如下.安装环境:操作系统:CentOS 7内存:11G ...
- 介绍Web项目中用到的几款JS日历日期控件和JS文本编辑框插件
第一款日历日期控件:layDate 官方网站:http://laydate.layui.com/ 第二款日历日期控件:my97 官方网站:http://www.my97.net/ 第三款 文本编辑器控 ...
- mysql 5.7快速部署
目录 一:官网下载mysql二级制包.... 1 二:mysql二级制包解压.... 1 三:设置mysql库文件路径与授权... 1 四. 创建配置文件... 2 五:数据库初始化... 5 六: ...
- Linux常用命令--文件(夹)查找之find命令
Linux系统用得越久,就会发现这真的是一个很优秀的系统,各种方便各种实用各种高效率. 晚饭前写一下find命令的笔记. 其实这篇笔记,也是看到一篇外文博客,写得不错,自己拿来练一练,然后才顺便写篇笔 ...
- Sqoop-将MySQL数据导入到hive orc表
sqoop创建并导入数据到hive orc表 sqoop import \ --connect jdbc:mysql://localhost:3306/spider \ --username root ...
- scala学习手记10 - 访问修饰符
scala的访问修饰符有如下几个特性: 如果不指定访问修饰符,scala默认为public: 较之Java,scala对protected的定义更加严格: scala可以对可见性进行细粒度的控制. s ...
- antd 表单双向绑定的研究
痛点 在使用antd的表单时,大家觉得不够清爽,总结如下: 大量的模板语法,需要一定的学习成本. 需要手动地进行数据绑定,使用大量的onChange/setFieldsValue去控制数据. 无法通过 ...
- jQuery EasyUI Datagrid VirtualScrollView视图简单分析
大家都知道EasyUI的Datagrid组件在加载大数据量时的优势并不是很明显,相对于其他一些框架,如果数据量达到几千,便会比较慢,特别是在IE下面.针对这种情况,我们首要做的是要相办法优化datag ...