【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 ...
随机推荐
- 摘记—Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha
What a Unicode string ? The binaries in RAM have the final word. NOT string literals in Text Editor( ...
- git使用(二)----创建版本库
创建版本库(操作都是在linux环境下) 什么是版本库呢?版本库又名仓库,英文名repository,其实就是一个目录,可以进行增删查改 创建一个目录,这里在根目录下创建一个git_home目录mkd ...
- [sh]uniq-sort-awk
题目:[百度搜狐面试题] 统计url出现次数 oldboy.log http://www.etiantain.org/index.html http://www.etiantain.org/1.htm ...
- 每日英语:March Remembers King's Dream
President Barack Obama on Wednesday said the entire world drew strength from the March on Washington ...
- jquery,checkbox无法用attr()二次勾选
今晨,漂亮的测试妹妹提了个奇怪的bug,说我一功能checkbox时隐时现,比如第一次打开有勾选,第n次打开可能就不选了. 想到与美女有亲密接触机会,马上鸡动起来. 经过偶层层抽次剥茧(da da j ...
- redis使用redis-cli查看所有的keys及清空所有的数据
redis_home:redis安装路径: cd %redis_home%/src ./redis-cli -h 127.0.0.1 127.0.0.1:6379> keys * (em ...
- style弹出带滚动条的虚拟窗口
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Python内置的数据类型--list,tuple
1. list Python内置的一种数据类型是列表:list. list是一种有序的集合,可以随时添加和删除其中的元素. 最后一个元素的索引是len(classmates) - 1,用-1做索引,直 ...
- android开发中用到的px、dp、sp
先介绍一下这几个单位: px : pixels(像素),相应屏幕上的实际像素点. dip :device independent pixels,与密度无关的像素,基于屏幕密度的抽象单位. 在每英寸16 ...
- Linux进阶:让效率翻倍的Bash技巧(一)
http://blog.tpircsboy.com/tech/bash-skills-part1/