【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 ...
随机推荐
- 【Android】11.4 Fragment及其生命周期
分类:C#.Android.VS2015: 创建日期:2016-02-22 一.简介 Android从3.0开始引入了fragment的概念,主要是为了支持在大屏幕上实现更为动态和灵活的UI设计,比如 ...
- iphone CGContextSetLineWidth 画线的问题
转自:http://blog.csdn.net/jxncwzb/article/details/6267154 CGContextRef context = UIGraphicsGetCurrentC ...
- 特效effects
Test中使用的特效如下 首先,使用ccg(x,y)建grid,一个Grid 属性就好像一个矩阵,是一个网络的线,组成一系列的方块和矩阵. 一个(16,12)大小的grid将会运行的非常快,但是并不会 ...
- MAC下cmake安装
可以参考网上手动下载cmake的源码包进行安装,http://www.cmake.org/download/ 解压后运行sudo ./bootstrap && sudo make &a ...
- Linux /proc目录详解
Linux系统上的/proc目录是一种文件系统,即proc文件系统.与其它常见的文件系统不同的是,/proc是一种伪文件系统(也即虚拟文件系统),存储的是当前内核运行状态的一系列特殊文件,用户可以通过 ...
- 使用konva来绘制一个矩形
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Java日志 (zhuan)
http://www.cnblogs.com/bird-li/p/4696662.html ************************************************* 日志对于 ...
- 基于jquery右侧悬浮加入购物车代码
分享一款基于jquery右侧悬浮加入购物车代码.这是一款基于jQuery实现的仿天猫右侧悬浮加入购物车菜单代码. 在线预览 源码下载 实现的代码: <!--左侧产品parabola.js控制 ...
- Hibernate xml配置方法之联合主键
1.StudentPK类,存放Student的联合主键,必须实现java.io.Serializable接口(为了序列化扩充移植),必须重写equals跟hashCode方法(为了确保唯一性) pub ...
- Android——通知 Notification
链接:http://jingyan.baidu.com/article/77b8dc7fde875a6175eab641.html http://www.2cto.com/kf/201502/3749 ...