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

知识点:BST的特点:

1、一个节点的左子树的全部点都小于或等于这个点的值。右子树的全部节点的值大于该节点的值;

2、最左节点值最小,最右节点值最大。

3、中序遍历结果值是一个非降的数列

问题:假设用Integer.MAX_VALUE会过不了測试用例,但是依照TreeNode中的定义。val值也是int呀。没办法用了Long。假设谁知道,麻烦解释一下哦。。

<span style="font-size:18px;">/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isValidBST(TreeNode root){
return helper2( root, Long.MAX_VALUE, Long.MIN_VALUE);
} private boolean helper2(TreeNode root, long maxValue, long minValue) {
if(root == null) return true;
if(root.val >= maxValue || root.val <= minValue) return false;
return helper2(root.left, root.val, minValue) && helper2(root.right, maxValue, root.val);
}
}</span>

【LeetCode】Validate Binary Search Tree 二叉查找树的推断的更多相关文章

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

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

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

  3. LeetCode: Validate Binary Search Tree [098]

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

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

  5. 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 ...

  6. LeetCode :: Validate Binary Search Tree[具体分析]

    Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less th ...

  7. [leetcode]Validate Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...

  8. Leetcode 笔记 98 - Validate Binary Search Tree

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

  9. leetcode dfs Validate Binary Search Tree

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

随机推荐

  1. C# 使用Nlog记录日志到数据库

    [摘要]Nlog是一个很不错的.NET日志记录组件,它可以将日志输出到控件台,保存到文本,也可以很方便的记录到数据库中.本文为你介绍C# 使用Nlog记录日志到数据库. Nlog是一个很不错的.NET ...

  2. C++windows内核编程笔记day13 进程、线程与信号量

    Windows进程 进程是一个容器,包括程序运行须要的代码.数据.资源等信息, windows进程的特点: 每一个进程都有自己的ID号 每一个进程都有自己的地址空间.进程之间无法訪问对方的地址空间. ...

  3. Linux操作系统及应用课程笔记 索引

    第0部分  软件的安装与配置 Linux下软件的安装与配置 第1部分*   绪论 第2部分*  Linux的安装过程 第3部分   系统Shell和经常使用命令 Shell文件相关经常使用命令及參数总 ...

  4. angular学习笔记(七)-迭代1

    本篇介绍angular中元素的迭代: <!DOCTYPE html> <html ng-app> <head> <title>4.1.迭代</ti ...

  5. tomcat 启动自动加载外部项目

    conf/server.xml 配置 <!--<Context path="/upload" docBase="E://upload" />- ...

  6. json demo

    package my.bigdata.movieTask.action; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.J ...

  7. C语言 · 核桃的数量

    历届试题 核桃的数量   时间限制:1.0s   内存限制:256.0MB        锦囊1 最小公倍数. 锦囊2 答案是a, b, c的最小公倍数.   问题描述 小张是软件项目经理,他带领3个 ...

  8. BUS Matrix

    ARM的BUS Matrix就是多主(Core,DMA等).多从(内部RAM,APB,外部总线等)的交联和仲裁.目的是为了提高不同主机访问不同外设情况下的带宽,另外一个就是简化Bus Master的协 ...

  9. Java中的 多线程编程

    Java 中的多线程编程 一.多线程的优缺点 多线程的优点: 1)资源利用率更好2)程序设计在某些情况下更简单3)程序响应更快 多线程的代价: 1)设计更复杂虽然有一些多线程应用程序比单线程的应用程序 ...

  10. 系统优化 /etc/sysctl.conf

    # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl ...