题目: 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. uva--242(邮资问题 dp)

    输入输出: id=26127" style="color:blue; text-decoration:none">Sample Input 5 2 4 1 4 12 ...

  2. 可靠的功能測试--Espresso和Dagger2

    欢迎Follow我的GitHub, 关注我的CSDN. 可靠的功能測试, 意味着在不论什么时候, 获取的測试结果均同样, 这就须要模拟(Mock)数据. 測试框架能够使用Android推荐的Espre ...

  3. JSP版LCX:端口转发神器 KPortTran

    最近接触内网很多,渗透过程中,由于windows和linux的差别以及运行语言环境的限制导致端口转发经常出现问题.于是自己写了个简单的JSP的端口转发脚本.仿造LCX的功能,具有正向.反向.监听三种模 ...

  4. C do {...} while (0) 在宏定义中的作用

    如果你是一名C程序员,你肯定很熟悉宏,它们非常强大,如果正确使用可以让你的工作事半功倍.然而,如果你在定义宏时很随意没有认真检查,那么它们可能使你发狂,浪费N多时间.在很多的C程序中,你可能会看到许多 ...

  5. Laravel4.2取得配置文件值

    Config::get('app.timezone'); laravel 使用Config::get方法来取得配置文件的值 laravel的配置文件的位置们于app.config文件夹的php文件中, ...

  6. How to add elements to a List in Scala

    Scala List FAQ: How do I add elements to a Scala List? This is actually a trick question, because yo ...

  7. 将Java项目从maven迁移到gradle

    将Java项目从maven迁移到gradle 如何将一个java项目从maven迁移到gradle呢?gradle集成了一个很方便的插件:Build Init Plugin,使用这个插件可以很方便地创 ...

  8. 简单的搭建php开发平台 WAMP

    下载wamp,地址http://www.wampserver.com/en/#download-wrapper 和正常软件安装下就行了. 修改WAMP中mysql默认空密码 WAMP安装好后,mysq ...

  9. Eclipse+tomcat+axis2进行web service部署

    用Eclipse+axis2+tomcat进行web service部署 2016-12-07 目录  1 安装JDK  1.1 下载JDK  1.2 安装和配置JDK  1.3 验证2 安装Ecli ...

  10. CTreeCtrl 父结点联动子结点CheckBox

    实现很简单,直接上代码: void CCheckBoxTreeDlg::OnNMClickTree1(NMHDR *pNMHDR, LRESULT *pResult) { // TODO: 在此添加控 ...