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. Matlab与C/C++联合编程之Matlab以MEX方式调用C/C++代码(一)

    MEX文件是一种可在matlab环境中调用的C语言(或fortran)衍生程序,mex的编译结果实际上就是一个带输出函数mexFunction 的dll文件. 中文名 mex文件 外文名 MATLAB ...

  2. rpm软件包管理/yum软件管理

    绝大数开源软件都是公布源代码的,源代码一般被打包为tar.gz归档压缩文件,然后手工编译为二进制可执行文件 ./configure 检查编译环境/相关库文件/配置参数,生成makefile make ...

  3. mysql 批量创建表,利用存储过程

    最近根据需求,需要提前创建一批日志表,以日期结尾,每天创建一张,例如XXX20160530,请参考如下: BEGIN    DECLARE `sName` VARCHAR(128);   DECLAR ...

  4. C#操作txt问件,进行清空添加操作

    //把txt清空 FileStream stream = File.Open(Adr,FileMode.OpenOrCreate,FileAccess.Write); stream.Seek(, Se ...

  5. javascript笔记1-基本概念

    关键字: 变量: function test(){ message = 'hi'; //不加var,表示全局变量:加var,表示局部变量 } 数据类型: 总共有五种基本数据类型:Undefined.N ...

  6. Java容器类List,ArrayList及LinkedList

    List容器类图 List是一个接口,它继承自Collection和Iterable,它的实现类有AbstractList,AbstrackSequenceList,ArrayList,LinkedL ...

  7. Linux---- vim 插件

    http://hi.baidu.com/omnice/blog/item/4ba97317cc67cc10962b4378.html fuzzyfinder.vim 可以替代四个插件, 不过这个插件也 ...

  8. LeetCode---- 二叉树中,找出和为某值的所有路径

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. LITTLE SHOP OF FLOWERS_DP

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20802   Accepted: 9613 Description You ...

  10. cocos2dx 搭建 android 平台

    Mac OS X下配置Cocos2d-x for Android(Eclipse)&IOS(Xcode)开发环境 前面一段时间只用Cocos2d-x在IOS平台下开发, 学习Cocos2d-x ...