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. Problem A 栈

    Description   You are given a string consisting of parentheses () and []. A string of this type is s ...

  2. 关于Mapper、Reducer的个人总结(转)

    Mapper的处理过程: 1.1. InputFormat 产生 InputSplit,并且调用RecordReader将这些逻辑单元(InputSplit)转化为map task的输入.其中Inpu ...

  3. Hadoop 重启各个节点

    对于datanode可以在master中配置,然后在maste启动的时候,一并去启动这些节点 .对于死掉的节点,也可以通过以下命令启动 .重启挂掉的节点进入到 挂掉的机器 bin/hadoop-dae ...

  4. JS内置对象

    字符串对象 <script> //字符串对象 var str = "Hello worldlsgjlsjg"; document.write('string.lengt ...

  5. windows防火墙添加规则

    #include <windows.h> #include <crtdbg.h> #include <netfw.h> #include <objbase.h ...

  6. 分析一个嵌入payload的恶意.lnk文件

    原文:https://isc.sans.edu/diary/Analyzis+of+a+Malicious+.lnk+File+with+an+Embedded+Payload/20763 We re ...

  7. oracle中的cluster表

    大家对通常oracle中的cluster的理解是不准确的,经常和sql server中的cluster index混淆.Cluster是存储一组table的一种方法,这些table共享同一数据块中的某 ...

  8. hdu 2061

    PS:  以为找个简单来恢复信心..结果碰到那么傻逼的题目... 题意:给出学分和成绩,算GPA...关键是注意换行....它要求的换行我觉得超级奇怪...除了第一个正常,其他的输入完之后先一个换行. ...

  9. hdu 2070

    ps:...递推..还是给出公式那种... 代码: #include "stdio.h" #define LL long long LL dp[]; int main(){ int ...

  10. 2016-1-10 手势解锁demo的实现

    一:实现自定义view,在.h,.m文件中代码如下: #import <UIKit/UIKit.h> @class ZLLockView; @protocol ZLLockViewDele ...