Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

Example 1:

    2
/ \
1 3

Binary tree [2,1,3], return true.

Example 2:

    1
/ \
2 3

Binary tree [1,2,3], return false.

判断一棵树是否是二叉搜索树。

判定范围即可。主要出问题的是出现在int的最大值,最小值附近。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class Solution {
int flag1 = 0;
int flag2 = 0;
public boolean isValidBST(TreeNode root) {
return isBST(root,Integer.MAX_VALUE,Integer.MIN_VALUE);
}
public boolean isBST(TreeNode root,int max,int min){
if( root == null )
return true;
if( root.val == Integer.MAX_VALUE && max == root.val ){
if( flag1 == 0){
flag1 = 1;
return isBST(root.right,max,root.val) && isBST(root.left,root.val,min);
}
else
return false;
}
if( root.val == Integer.MIN_VALUE && min == root.val ){
if( flag2 == 0){
flag2 = 1;
return isBST(root.right,max,root.val) && isBST(root.left,root.val,min);
}
else
return false;
}
if( root.val <=min || root.val >= max)
return false;
return isBST(root.right,max,root.val) && isBST(root.left,root.val,min); }
}

所以可以稍微优化一下。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/ public class Solution {
public boolean isValidBST(TreeNode root) {
return dfs(root, (long)(Integer.MIN_VALUE)-1, (long)(Integer.MAX_VALUE)+1);
}
private boolean dfs(TreeNode root, long gt, long lt){
if(root == null) return true;
if(root.val >= lt || root.val <= gt) return false;
return dfs(root.left, gt, root.val) && dfs(root.right, root.val, lt);
} }

leetcode 98 Validate Binary Search Tree ----- java的更多相关文章

  1. [LeetCode] 98. Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  2. Leetcode 98. Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  3. [leetcode]98. 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] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆

    描述 解析 二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n. 节点n的右孩子所在的树,每个节点都大于节点n. 定义子树的最大最小值 比如:左孩子要小于父节点:左孩子n的右孩子要大于n ...

  5. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  6. 【LeetCode】98. Validate Binary Search Tree (2 solutions)

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

  7. 【leetcode】Validate Binary Search Tree

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

  8. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  9. 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...

随机推荐

  1. POJ 1845 求a^b的约数和

    题目大意就是给定a和b,求a^b的约数和 f(n) = sigma(d) [d|n] 这个学过莫比乌斯反演之后很容易看出这是一个积性函数 那么f(a*b) = f(a)*f(b)  (gcd(a,b) ...

  2. K2 BPM + SAP,实现全方面管理企业

    K2作为专业的BPM.工作流管理平台供应商,面向庞大的SAP用户群体,除了提供产品化的SAP集成工具「K2 connect」产品之外,更拥有一套得到众多客户验证的集成解决方案. 此方案可供SAP用户或 ...

  3. PDF2SWF转换只有一页的PDF文档,在FlexPaper不显示解决方法

    问题:PDF2SWF转换只有一页的PDF文档,在FlexPaper不显示! FlexPaper 与 PDF2SWF 结合是解决在线阅读PDF格式文件的问题的,多页的PDF文件转换可以正常显示,只有一页 ...

  4. MapReduce基础

    这篇文章翻译自Yahoo的Hadoop教程,很久之前就看过了,感觉还不错.最近想总结一下以前学的东西,看到现在关于Hadoop的中文资料还比较少,就有了把它翻译出来的想法,希望能帮助到初学者.这只是Y ...

  5. MongoDB 聚合 (转) 仅限于C++开发

    MongoDB除了基本的查询功能,还提供了很多强大的聚合工具,其中简单的可计算集合中的文档个数, 复杂的可利用MapReduce做复杂数据分析. 1.count count返回集合中的文档数量 db. ...

  6. rcc

    一.在STM32中,有五个时钟源,为HSI.HSE.LSI.LSE.PLL.   全名: high  speed  external ①HSI是高速内部时钟,RC振荡器,频率为8MHz. ②HSE是高 ...

  7. 安全增强 Linux (SELinux) 剖析

    架构和实现 Linux® 一直被认为是最安全的操作系统之一,但是通过引入安全增强 Linux(Security-Enhanced Linux,SELinux),National Security Ag ...

  8. Pattern和Matcher

    java util本身提供了Pattern和Matcher(java.util.regex.Pattern,Matcher),两个类均是与正则表达式相关的类,其中: java.util.regex是一 ...

  9. css3制作六边形图片

    效果图: 实现原理: 这个效果的主要css样式有: 1.>transform: rotate(120deg); 图片旋转 2.>overflow:hidden;  超出隐藏 3.>v ...

  10. 用python做些有意思的事——分析QQ聊天记录——私人订制

    之前,写了这篇文章,用python提取全部群成员的发言时间,并简单做了下分析.先补充一下,针对特定单个群成员(这里以  小小白   为例)消息记录的获取. 代码比较简单,主要是正则表达式的书写.(附: ...