[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 ...
随机推荐
- android开发工具类之获得WIFI IP地址或者手机网络IP
有的时候我们需要获得WIFI的IP地址获得手机网络的IP地址,这是一个工具类,专门解决这个问题,这里需要两个权限: <uses-permission android:name="and ...
- C# ADO.NET参数查询
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 解读Spring Ioc容器设计图
在Spring Ioc容器的设计中,有俩个主要的容器系列:一个是实现BeanFactory接口的简单容器系列,这系列容器只实现了容器最基本的功能:另外一个是ApplicationContext应用上下 ...
- LSJ_NHibernate第一章 NHibernate介绍
前言: 说起NHibernate网上资料真不少,但用的人却并不多,说起java的SSH框架大家可能就知道了,这里的H指的就是Hibernate,一款好用的ORM框架,在.net重写了这款好用的框架,名 ...
- EF的TransactionScope
TransactionScope是一个分布式事务的语句块,被包含起来的语句一起被提交,当出现异常,一起回滚,这都是托管的 当Web没有开启MSDTC服务时候会出现:
- bootstrap paginator 与 bootstrap3兼容
bootstrap paginator可支持bootstrap2 和bootstrap3. 默认的下载包中支持2,需要手动修改才能支持bootstrap3.具体方法:找到bootstrap-pagin ...
- iOS开发——免证书调试(Xcode7,iOS9)
(资料已做好,待整理成文章……)
- iOS开发——音频篇——音效的播放
一.简单介绍 简单来说,音频可以分为2种 (1)音效 又称“短音频”,通常在程序中的播放时长为1~2秒 在应用程序中起到点缀效果,提升整体用户体验 (2)音乐 比如游戏中的“背景音乐”,一般播放时间较 ...
- c# linq 基础知识点
1.Where与TakeWhile,Where和sql中的where一样,返回所有满足条件的元素,而TakeWhile判断原理类似于while语句,从头逐个判断,只要条件为真就一直返回检索到的元素,只 ...
- sublime_text编辑器下载安装使用
1.sublime_text下载 https://www.sublimetext.com/3 (选择相对应的版本) 2.通过package control安装插件 https://packagec ...