[Leetcode] Validate BST
给一个Binary Tree,检查是不是Binary Search Tree. 即是否满足对每个节点,左子树的中的所有节点的值 < 当前节点的值 < 右子树所有节点的值。
Solution #1, 用中序遍历。因为中序遍历是DFS的一种,Time complexity: O(N), space complexity: O(logN)
public class Solution { int lastCheck = Integer.MIN_VALUE; public boolean isValidBST(TreeNode root){
if(root == null)
return true; if(!isValidBST(root.left)){
return false;
} if(lastCheck >= root.val){ // only 'Less than' is valid
return false;
} lastCheck = root.val; return isValidBST(root.right);
} }
Solution #2:
为每个节点施加一个取值范围 (min, max). 从根节点一步一步往下递归的时候不断的更新(缩小)这个范围。
class Solution{ public boolean isValidBST(TreeNode node){
return isValidBST(node, Integer.MAX_VALUE, Integer.MIN_VALUE);
} private boolean isValidBST(TreeNode node, int max, int min){
if(node == null)
return true; if(min < node.val && node.val < max){
return isValidBST(node.left, node.val, min) &&
isValidBST(node.right, max, node.val);
}else{
return false;
}
} }
[Leetcode] Validate BST的更多相关文章
- [LeetCode] Largest BST Subtree 最大的二分搜索子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- Leetcode: Largest BST Subtree
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- [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] Convert BST to Greater Tree 将二叉搜索树BST转为较大树
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- [LeetCode] Split BST 分割二叉搜索树
Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- [Cracking the Coding Interview] 4.5 Validate BST
Implement a function to check if a binary tree is a binary search tree. 这道题很经典,让我们判断一棵树是不是二叉查找树.但是首先 ...
- [LeetCode] Validate IP Address 验证IP地址
In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...
- [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 ...
随机推荐
- Clean Code(三):注释
在工作中经常在刚开始写代码的时候,写下类与方法的注释,时间久了,CR多了,也不可能一直去修改注释了.加班都累成dog.注释才不改呢,就是这么任性,哈哈. 项目久了,注释会欺骗阅读者,本人被骗过,也骗过 ...
- hibernate篇章三-- hibernate配置文件hibernate.cfg.xml的详细解释
<!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式--> <?xml version='1.0' ...
- SQL Server 2008导入、导出数据库
SQL Server 2008数据库的导入.导出和Mysql的导出还有一定的区别,刚开始的时候完全摸不到方向,利用Microsoft SQL Server Management Studio进行导入. ...
- updatepannel的使用
注意:放在updatepannel中的数据,单击事件之后,只重新加载后台数据,不加载前台数据,所以如果页面上有js的插件,对js插件的引用和赋值不要放在updatepannel中,同时尽量减小upda ...
- 20151225jquery学习笔记---选项卡UI
圣诞节快乐,哈哈哈....选项卡(tab),是一种能提供给用户在同一个页面切换不同内容的 UI. 尤其是在页面布局紧凑的页面上,提供了非常好的用户体验.一. 使用 tabs使用 tabs 比较简单,但 ...
- VIM学习1
不得不说鸟哥的Linux写得太好了,VIM篇章,通读一篇,感觉收获挺大.之前几年前装逼硬着学,感觉硬是没懂,看的特晕,学得特别慢,抄一两遍也没什么多大的作用.这一回看了,感觉马上就能记住不少,当然大多 ...
- JQ异步调用
AjaxGet请求方式: <script type="text/javascript"> $.ajax({ type: "GET", dataTyp ...
- SQL Server调优系列进阶篇 - 查询优化器的运行方式
前言 前面我们的几篇文章介绍了一系列关于运算符的基础介绍,以及各个运算符的优化方式和技巧.其中涵盖:查看执行计划的方式.几种数据集常用的连接方式.联合运算符方式.并行运算符等一系列的我们常见的运算符. ...
- springmvc中url-pattern的大坑
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springfr ...
- dbms_job涉及到的知识点
用于安排和管理作业队列,通过使用作业,可以使ORACLE数据库定期执行特定的任务. 一.dbms_job涉及到的知识点1.创建job:variable jobno number;dbms_job.su ...