[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 ...
随机推荐
- 【iOS程序启动与运转】- RunLoop个人小结
学习iOS开发一般都是从UI开始的,从只知道从IB拖控件,到知道怎么在方法里写代码,然后会显示什么样的视图,产生什么样的事件,等等.其实程序从启动开始,一直都是按照苹果封装好的代码运行着,暴露的一些属 ...
- hdu1016JAVA
import java.util.Arrays;import java.util.Scanner;public class Main { public static int kase=0,n; pub ...
- JS火狐与IE的差别
function isIE(){ //ie? ) return true; else return false; } if(!isIE()){ //firefox innerText define H ...
- Android和Java的轻巧Wire协议缓冲器
Wire协议缓冲器 一个人必须有一个代码! -奥马尔小 由于我们的团队和项目增长,数据的种类和数量也随之增加. 成功将您简单的数据模型转换为复杂的! 无论您的应用程序将数据存储到磁盘或网络传送信号,该 ...
- 纯js写“运动”框架
所谓“运动”不一定真的是运动,在连续的一段时间内改变某一样式都可以成为“运动”. 先写几个会用到的函数 //获取某一元素的指定样式 function getstyle (element, target ...
- JAVA Oauth 认证服务器的搭建
http://blog.csdn.net/binyao02123202/article/details/12204411 1.软件下载 Oauth服务端: http://code.google.com ...
- pull解析xml文件
pull解析xml文件 先自己写一个xml文件,存一些天气信息 拿到xml文件 InputStream is = getClassLoader().getResourceAsStream(" ...
- gdal中文路径无法打开问题
在C#中使用OGR读写矢量数据时,需要引用“using OSGeo.OGR;”. 同时为了处理中文路径和中文字段,需要在开始设置下面两个属性,代码如下: //为了支持中文路径,请添加下面这句代码(大多 ...
- OC - 2.OC基础知识介绍
一.基础语法 1> OC语言和C语言 C语言是面向过程的语言,OC语言是面向对象的语言 OC语言继承了C语言,并增加了面向对象的思想 以下内容只介绍OC语言与C语言的不同之处 2> 关键字 ...
- 04_XML_02_XML语法
[XML组成] 一个XMl文件分为以下几部分组成 * 文档说明 * 元素 * 属性 * CDATA区.特殊字符 * 处理指令(processing Instruction) [1.文档说明] * 最简 ...