【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 ...
随机推荐
- 聊天server-解密陌生人(10)位置管理和获取周围一公里陌生人
提示: 由于project略微有点大对我个人来说.所以可能在某些方面讲的不清楚或逻辑性不够强,假设有问题请@我. 原project:https://github.com/LineChen/ 六.用户位 ...
- [svc]centos7安装优化最佳姿势
源/软件/时区/selinux/ulimit/vim/sshd 下载安装: https://mirrors.aliyun.com/centos/7.4.1708/isos/x86_64/CentOS- ...
- Java8 新特性简介
Java8是2014年发布的,至今也已经有快三年的时间了,之前虽然有学习过,但是学的比较零散,不成系统,而且也没有覆盖到Java8所有的特性. 由于公司已经使用了JDK1.8,所以工作中能使用Java ...
- Python 訪问Google+ (http)
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-28 @author: guaguastd @name: l ...
- iOS自定义组与组之间的距离以及视图
iOS自定义组与组之间的距离以及视图 //头视图高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(N ...
- iOS开发之地域选择
代码地址: https://github.com/boundlessocean/AreaPickViewDome.git 效果: 第一步: 将BLAreaPickerView 导入工程 第 ...
- Oracle PLSQL Demo - 22.查看字符串的长度[lengthb, length],判断字符串是否包含中文
--Count the length of string select lengthb('select * from scott.emp') as countted_by_byte, length(' ...
- nonatomic对引用计数的影响(非ARC)
@interfaceAppDelegate() { NSObject * obj_; } @property(retain) NSObject * obj;// 默认是atomic //@proper ...
- windows 7/mac编译cocos2d-x-3.2*的android工程报错
开始学习cocos2d-x-3.* 凭着对2.*的各个版本的认识和升级的经验,本以为直接用最新的3.2rc0版本练手应该没有问题,结果一上来就是一个大坑.你妹! Android NDK: Invali ...
- WPF学习之X名称空间详解
X名称空间里面的成员(如X:Name,X:Class)都是写给XAML编译器看的.用来引导XAML代码将XAML代码编译为CLR代码. 4.1X名称空间里面到底都有些什么? x名称空间映射的是:htt ...