题目: 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. angular学习笔记(五)-阶乘计算实例(2)

    <!DOCTYPE html> <html ng-app> <head> <title>2.3.3计算阶乘实例2</title> <m ...

  2. LeetCode: Gray Code 解题报告

    Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit ...

  3. js图片转base64并压缩

    /* 2015-09-28 上传图片*/ function convertImgToBase64(url, callback, outputFormat){ var canvas = document ...

  4. c++重载前置++和--

    C语言中,前置和后置++,--都不能作为左值,而在c++中,前置的++和--可以作为左值,从下面的重载运算符中也可以看出,它们返回的是引用,我不知道为什么这里和c语言中不同,但c++类似的提升还有三目 ...

  5. JavaScript高级 面向对象(1)--添加一个div标签

    说明(2017.3.28): 1. JavaScript是一种基于对象的多范式编程语言,不是面向对象,但离开对象不能活. 范式编程是指编程习惯.方式,分为过程式.对象式和函数式编程. 2. 面向对象是 ...

  6. IOS之TableViewCell重用机制避免重复显示问题

    常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复. 1 // 这样配置的话超过页面显示的内容会重复出现 2 - (UITableViewCell *)tableVi ...

  7. xml选择节点方法

    1.选取某个节点 方法一:newNode = document.DocumentElement.SelectSingleNode("//student[@id='A103']"); ...

  8. 【C#】往异步下载的方法传递自定义完成事件

    封装自定义的异步下载方法时,正常情况下是这样的: /// <summary> /// 异步方法:联网下载文件,保存到本地. /// </summary> /// <par ...

  9. 【SpringMVC笔记】第一课-框架执行过程

    SpringMVC模型的执行流程

  10. FluentData - 轻量级.NET ORM持久化技术解决方式

    FluentData - 轻量级.NET ORM持久化技术解决方式   文件夹:    一.什么是ORM?  二.使用ORM的优势  三.使用ORM的缺点  四.NET下的ORM框架有哪些?  五.几 ...