【LeetCode】Validate Binary Search Tree 二叉查找树的推断
题目: 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 二叉查找树的推断的更多相关文章
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- [leetcode]Validate Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/ 题意:检测一颗二叉树是否是二叉查找树. 解题思路:看到二叉树我们首 ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
随机推荐
- VS2008配置OpenGl 亲测可行
OpenGL作为当前主流的图形API之一,它在一些场合具有比DirectX更优越的特性.1.与C语言紧密结合.OpenGL命令最初就是用C语言函数来进行描述的,对于学习过C语言的人来讲,OpenGL是 ...
- php读取sqlite数据库入门实例
php读取sqlite数据库的例子,php编程中操作sqlite入门实例.原文参考:http://www.jbxue.com/article/php/22383.html在使用SQLite前,要确保p ...
- linux系统下添加新硬盘的方法详解
对于linux新手来说,在linux上添加新硬盘,是很有挑战性的一项工作. 在Linux服务器上把硬盘接好,启动linux,以root登陆. fdisk -l ## 这里是查看目前系统上有几块硬盘 D ...
- 批处理学习笔记7 - 管道连接符"|"
|就是把左边作为值传递给右边.有一些命令运用它比较方便 @echo off ping baidu.com | find "TTL" pause 这段命令就是把左边ping的结果传递 ...
- iOS-ARC-环境下如何查看引用计数的变化
iOS-ARC-环境下如何查看引用计数的变化 一,新建立一个工程,用于测试引用计数的变化. 二,找到如下路径Build Phases---->Compile Sources---->App ...
- Python+Django+js+echarts引入本地js文件的操作方法
1. 选择正确的echarts.js,开发版选择echarts.baidu.com上的源码版,避免出现问题 2. 在项目主目录中新建static文件夹,里面建立js.css.images文件夹 3. ...
- ClouderaManager之CDH-LZO配置
CDH-LZO配置 下载和CDH版本对应的hadoop-lzo版本 如下: 下载地址:http://archive.cloudera.com/gplextras5/parcels/ 需要下载如下三个文 ...
- (HttpURLConnection)强制转化
HTTP的请求详解在我的博客中已经讲解过: http://blog.csdn.net/xiazdong/article/details/7215296 我在http://blog.csdn.net/x ...
- JavaScript高级 面向对象(8)--浅拷贝代码实现
说明(2017.3.31): 1. 浅拷贝,只有值属性,没有引用属性. 2. 在原对象里面添加一个copy方法,返回本对象内的所有值属性. <!DOCTYPE html> <html ...
- VBA学习笔记(2)--新建word文档并插入文字
说明(2017.3.20): 1. Dim As声明变量类型,Set赋值/初始化 2. With使后面的省略对象,直接点就行,后面要End With 3. Application.StatusBar ...