[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 ...
随机推荐
- 【转】MyEclipse第一个Servlet程序
转自:http://blog.csdn.net/wangdingqiaoit/article/details/7674367 前言 本文旨在帮助学习java web开发的人员,熟悉环境,在Win7系统 ...
- Android 开发第二天
开发入门HelloWorld 首先打开开发工具 第一步 第二步 效果图 以后可以点击一直下去 第三步骤介绍一下里面项目的作用 SRC是用来保存源代码的东西MainAcrivity.java主视图res ...
- android中实现简单的播放
MediaPlayer mediaPlayer1; mediaPlayer1 = MediaPlayer.create(getBaseContext(), R.raw.ic_yanyuan); med ...
- 通过虚拟机VMware来练习安装ESXi
关于VMware vSphere组件ESXi,大家请自行百度.大概的意思我简单的先理解为这个组件是通过在服务器上安装上ESXi系统,继而虚拟化整个服务器的硬件资源为之后虚拟各种客户端所用.相比较大家较 ...
- 图解I/O的五种模型
1.1 五种I/O模型 1)阻塞I/O 2)非阻塞I/O 3)I/O复用 4)事件(信号)驱动I/O 5)异步I/O 1.2 为什么要发起系统调用? 因为进程想要获取磁盘中的数据,而能和磁盘打交道的只 ...
- BS开发平台,一小时搞定功能强大的统计分析页面
BS开发平台,一小时搞定功能强大的统计分析页面,拥有强大的功能和详细的权限控制. 1.组织数据,分析需求(实际耗时大约20分钟) 2.建立需要的业务数据表(大致10分钟)3. 运行代码工具,生产需要 ...
- Devexpress 使用经验 —— ASPxGridView命令行自定义按钮灵活使用
ASPX <dx:ASPxGridView ID="ASPxGridView1" runat="server" DataSourceID="Ob ...
- OC加强-day05
#program mark - 0_11 NSRange结构体介绍 [掌握] 是Foundation框架中的一个结构体 NSRange 定义的一个变量的两个属性 location:起始下标 lengt ...
- swift 自行理解
- switch case实现两个数的算术运算
方法一: package com.liaojianya.chapter1; import java.util.Scanner; public class SwitchDemo1 { public st ...