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. Deep Learning 初识

    实际生活中,人们为了解决一个问题,如对象的分类(对象可是是文档.图像等),首先必须做的事情是如何来表达一个对象,即必须抽取一些特征来表示一个对象,如文本的处理中,常常用词**来表示一个文档,或把文档表 ...

  2. Java 类的一般特征

    1. 创建类的对象时的内存结构 用图来解释: 使用new 创建 a1 时,成员变量的值都是初始默认值. 然后显式的改变其属性的值. 创建a3 时,a3 是直接指向 a1, 即a3 = a1, 两个对象 ...

  3. Rhel6-tomcat+nginx+memcached配置文档

    理论基础: User - > web ->nginx  ->tomcat1 ->*.jsp 80          8080 ↓      -> tomcat2 html ...

  4. SVN Unable to connect to a repository at URL

    方法一:右键菜单的“TortoiseSVN”->“Settings”->“Save Data”对话框中,点击“Authentication data”旁的“Clear”按钮,清除登录凭证. ...

  5. 【转发】RedHat Enterprise Linux 6.4 使用 Centos 6 的yum源问题

    作为一名新手,学习Linux已经一个月了,其间遇到了不少问题,而今天笔者遇到的问题是 #yum install pam-devel #This system is not registered to ...

  6. Introduction to Machine Learning

    Chapter 1 Introduction 1.1 What Is Machine Learning? To solve a problem on a computer, we need an al ...

  7. Julia中文教程资源.txt

    Julia中文教程资源.txt 2016年3月28日 05:18:32 codegay 本文更新在这里: https://github.com/FGFW/julia-science-and-techn ...

  8. python 接口开发(一)

    cmd中,提示pip版本太低,先升级pip   pip install --upgrade pip (pip,安装和管理python扩展包的工具) cmd下,pip,出现详细信息证明装成功了 pip ...

  9. 《day16_多线程细节_Eclipse使用》

    多线程的一些细节: 1,面试题:sleep方法和wait方法异同点是什么? 相同点:可以让线程处于冻结状态. 不同点: 1, sleep必须指定时间. wait可以指定时间,也可以不指定时间. 2, ...

  10. 重拾java系列一java基础(2)

    1.分支流程控制 if(布尔表达式/分支条件){  //语句块} if(布尔表达式/分支条件){  //语句块1}else{  //语句块2} if(条件1){  //语句块1}else if(条件2 ...