给一个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的更多相关文章

  1. [LeetCode] Largest BST Subtree 最大的二分搜索子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  2. Leetcode: Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] Split BST 分割二叉搜索树

    Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two ...

  6. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  7. [Cracking the Coding Interview] 4.5 Validate BST

    Implement a function to check if a binary tree is a binary search tree. 这道题很经典,让我们判断一棵树是不是二叉查找树.但是首先 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. How to add route for IPV6 interface

    Firewall : ifconfig eth1 inet6 add 2000::1/64 ifconfig eth4 inet6 add 5000::1/64 ------------------- ...

  2. 在ModelSim波形图中以参数名显示变量

    在ModelSim波形图中以参数名显示变量 在使用Verilog HDL编写有限状态机等逻辑的时候,状态机的各个状态通常以参数表示,但当使用ModelSim仿真的时候,状态机变量在wave窗口中以二进 ...

  3. 4柱汉诺塔(zz)

    多柱汉诺塔可以用Frame–Stewart算法来解决. The Frame–Stewart algorithm, giving a presumably optimal solution for fo ...

  4. js选中checkbox赋值给文本框

    //js $(function(){ var name=""; var kmname=[]; var n= $("input[type=checkbox]"). ...

  5. 面向服务的体系结构(service-oriented architecture,SOA)

    SOA的概念是Gartner 在1996年提出来的,并于2002年12月进一步提出SOA是“现代应用开发领域最重要的课题”.   一.SOA的定义 SOA分为广义的SOA和狭义的SOA,广义的SOA是 ...

  6. MVC小系列(二十二)【MVC的Session超时,导致的跳转问题】

    由于mvc内部跳转机制的问题,它只在当前的action所渲染的view上进行跳转,如果希望在当前页面跳,需要将mvc方法改为js方法: filterContext.Result = new Redir ...

  7. WPF窗体视图中绑定Resources文件中字符串时,抛出:System.Windows.Markup.StaticExtension

    问题描述: 在Resources.resx定义了一个静态字符串字段Title,并在WPF窗体视图中绑定为窗体的标题: Title="{x:Static local:Resources.Tit ...

  8. Saltstack安装配置(一)

    一.服务端和客户端安装 1.下载epel源 http://mirrors.zju.edu.cn/epel/6/ #wget http://mirrors.zju.edu.cn/epel/6/x86_6 ...

  9. 误删除了Oracle的dbf文件后的解决方法

    问题描述: 误删除Oracle数据库的dbf文件,在启动和关闭数据库是会提示错误. startup启动数据库时提示: ORA-01157:无法标识/锁定数据文件 ORA-01110:数据文件:‘... ...

  10. iOS开发中常用的分类方法---UIImage+Category

    在开发中使用分类对原有的系统类进行方法扩展,是增强系统原有类功能的常见做法. /** * 自由拉伸一张图片 * * @param name 图片名字 * @param left 左边开始位置比例 值范 ...