【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 ...
随机推荐
- Windows Azure Mobiles Services实现client的登录注冊
下文仅仅是简单实现,client以Android端的实现为例: 用户表Account: package com.microsoft.ecodrive.model; public class Accou ...
- 微信开发,对象转换为xml时候引用XStream这个类报错处理方案
报错的信息为:The type org.xmlpull.v1.XmlPullParser cannot be resolved. /** * 扩展XStream 支持CDATA */ privat ...
- mac重装系统
通过 macOS 恢复功能启动macOS 恢复功能会根据您在电脑启动时按下的组合键来安装不同版本的 macOS.在按电源按钮打开 Mac 后,立即按住以下组合键之一.然后在看到 Apple 标志或旋转 ...
- JS正则表达式获取分组内容实例
JS正则表达式获取分组内容. 支持多次匹配的方式: var testStr = "now test001 test002"; var re = /test(\d+)/ig; var ...
- 飘逸的python - 几行代码实现unix管道风格的函数调用
用过linux的基本知道它的管道,是将一个程序或命令的输出作为还有一个程序或命令的输入. 废话少说,以下我们看用python怎么实现unix管道风格的函数调用. #coding=utf-8 class ...
- 可靠的功能測试--Espresso和Dagger2
欢迎Follow我的GitHub, 关注我的CSDN. 可靠的功能測试, 意味着在不论什么时候, 获取的測试结果均同样, 这就须要模拟(Mock)数据. 測试框架能够使用Android推荐的Espre ...
- In search of the perfect URL validation regex
To clarify, I’m looking for a decent regular expression to validate URLs that were entered as user i ...
- 让一个div始终固定在页面的某一固定位置的方法
方法一:直接用position:fixed 方法二:写一个滚动条滚动事件,让这个div设置 position:absolute 该top的距离等于滚动的距离scrollTop() 写法如下:$(win ...
- LeetCode: Median of Two Sorted Arrays 解题报告
Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find t ...
- C#处理文本文件TXT实例详解(转)
作者:安静平和 字体:[增加 减小] 类型:转载 时间:2015-02-02我要评论 这篇文章主要介绍了C#处理文本文件TXT的方法,以实例形式详细分析了txt文本文件的读取.修改及打印等功能的实现技 ...