[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 ...
随机推荐
- AFNetworking 3.0迁移指南
AFNetworking是一款在OS X和iOS下都令人喜爱的网络库.为了迎合iOS新版本的升级, AFNetworking在3.0版本中删除了基于 NSURLConnection API的所有支持. ...
- Interesting visualization tools for profiling.
Interesting visualization tools for profiling. http://dtrace.org/blogs/brendan/2012/03/17/linux-kern ...
- Windows下安装破解JIRA6.3.6
相关工具下载地址:http://pan.baidu.com/s/1kT9xZEJ 安装环境: WindowsXP MySQL-5.5.28 JDK1.6.0_21 JIRA功能全面,界面友好,安装简单 ...
- Java基础知识强化之IO流笔记62:三种方式实现键盘录入
1. 三种方式实现键盘录入 System.in 标准输入流.是从键盘获取数据的 键盘录入数据三种方式: A:main方法的args接收参数. java HelloWorld hello w ...
- android和javascript之间相互通信实例分析
1. AndroidManifest.xml中必须使用许可 "android.permission.INTERNET", 否则会出Web page not available错误 ...
- iOS开发篇-申请开发者账号流程
1.注册一个苹果的apple id申请apple id的地址: https://appleid.apple.com/account 2.如申请公司账号,请使用以下链接免费获取邓白氏号码,以下的申请表格 ...
- 怎么在AptanaStudio中把电脑中的浏览器设置为默认执行方法
1.在Aptana中找到按钮 2.点击向下的三角形找到
- c语言,strcmpi(),将一个串中的一部分与另一个串比较, 不管大小写
#include<stdio.h> #include<string.h> 函数名: strncmpi 功 能: 将一个串中的一部分与另一个串比较, 不管大小写 用 法: int ...
- hdu 1563 Find your present!
Find your present! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Objective-c中autorelease的释放时机
如果你使用过MRR,autorelease这个关键字应该是太熟悉了,每次在我们生成一个新的对象返回时,都需要向这个对象发送autorelease消息,目的是为了延时释放创建的对象.那到底是在什么时候, ...