LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)
题目:验证二叉搜索树
难度:Medium
题目内容:
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
翻译:
给定一棵二叉树,确定它是否是一个有效的二叉搜索树(BST)。
BST的定义如下:
节点的左子树只包含小于节点键的键节点。
节点的右子树只包含大于节点键的键节点。
左和右子树都必须是二叉搜索树。
Example 1:
Input:
2
/ \
1 3
Output: true
Example 2:
5
/ \
1 4
/ \
3 6
Output: false
我的思路:搜索二叉树的验证就是要求每一个子树都是满足搜索树的“左小右大”的规定,
1、先判断自己作为根节点的左右二叉是否符合;
2、然后返回左右节点的递归结果的 “与” (全都符合才算符合)
我的代码:
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
if (root.left != null) {
TreeNode cur = root.left;
while (cur.right != null) {
cur = cur.right;
}
if (cur.val >= root.val) {
return false;
}
}
if (root.right != null) {
TreeNode cur = root.right;
while (cur.left != null) {
cur = cur.left;
}
if (cur.val <= root.val) {
return false;
}
}
return isValidBST(root.left) && isValidBST(root.right);
}
我的复杂度:O(N*logN)
编码过程中的问题:
1、没仔细看题,而且记错搜索树的定义了,当出现两个值相等的时候,此时不是搜索树;
2、最开始只考虑到判断根节点和左右两个子节点就行了,结果后面案例跑错了,例如:
5
/ \
1 6
/ \
3 7 每个子树都是正确的二叉搜索树,但是整体上看,5的右子树内有比它小的3,所以此树不是二叉搜索树。
此时想到二叉树删除算法,当删除节点有两个子节点的时候,此时会选择此节点左节点的最右子系节点,或者右节点的最左子系节点进行代替,所以这两个节点值才是最接近根节点值的节点,所以每次的单个子树判断应该判断这两个,而不是左右子节点就行了。
答案代码:
public boolean isValidBST(TreeNode root) {
return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
}
public boolean isValidBST(TreeNode root, long minVal, long maxVal) {
if (root == null) return true;
if (root.val >= maxVal || root.val <= minVal) return false;
return isValidBST(root.left, minVal, root.val) && isValidBST(root.right, root.val, maxVal);
}
答案复杂度:O(N)
答案思路:
也是利用了递归的思想,分别对每一个子树进行判断,但是它的亮点在于在判断的时候并不需要对子树进行搜索“最相近的值”,而是利用了“最大值”、“最小值”的思想:
对于每个子树,都有一个最大值和一个最小值,
对于左子树,最大值就是它的根节点的值,最小值是根节点的最小值(左父亲或者MIN_VALUE)
对于右子树,最小值就是它的根节点的值,最大值是根节点的最大值(右父亲或者MAX_VALUE)
例如:
5
/ \
1 6
/ \
3 7
5的满足小于最大值,大于最小值,然后递归(1,MIN,5) && 递归(4,5,MAX)
。。。
3节点的最大值为6,最小值应该为5,此时不满足,所以return false 其实还有一种非递归的解法:中序遍历,利用二叉搜索树中序遍历的有序性(在中序遍历的出栈时判断此值是否小于之前出栈的那个节点的值)
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
Stack<TreeNode> stack = new Stack<>();
TreeNode pre = null;
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
if(pre != null && root.val <= pre.val) return false;
pre = root;
root = root.right;
}
return true;
}
注意:不能使用出栈值与栈顶进行比较,因为在中序遍历的过程中栈顶可能为空,所以此时无法比较。
LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)的更多相关文章
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- [LeetCode] 98. 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] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [leetcode]98. Validate Binary Search Tree验证二叉搜索树
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 098 Validate Binary Search Tree 验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树.一个二叉搜索树有如下定义: 左子树只包含小于当前节点的数. 右子树只包含大于当前节点的数. 所有子树自身必须也是二叉搜索树.示例 1 ...
- Leetcode98. Validate Binary Search Tree验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索 ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode98]98. Validate Binary Search Tree判断二叉搜索树
判断二叉搜索树的方法是: 中序遍历形成递增序列 //全局变量记录中序遍历产生的序列,因为要递归,所以要用全局变量 List<Integer> list = new ArrayList< ...
- [leetcode]108. Convert Sorted Array to Binary Search Tree构建二叉搜索树
构建二叉搜索树 /* 利用二叉搜索树的特点:根节点是中间的数 每次找到中间数,左右子树递归子数组 */ public TreeNode sortedArrayToBST(int[] nums) { r ...
随机推荐
- SoftReference、WeakReference、PhantomRefrence分析和比较
级别 什么时候被垃圾回收 用途 生存时间 强引用 从来不会 对象的一般状态 JVM停止运行时终止 软引用 在内存不足时 优化内存使用 内存不足时终止 弱引用 在垃圾回收时 对象缓存 gc运行后终止 虚 ...
- SpringMVC读取配置文件
源文件 pay.properties: inputCharset=1 receiveUrl=www.baidu.com version=v1.0 language=1 signType=1 merch ...
- 常用WebSite Address
百度 魅族深度学习应用大赛 byr论坛 帖子 https://bbs.byr.cn/#!article/ML_DM/24852?p=1 百度 魅族深度学习应用大赛 官网 http://meizu.ba ...
- EditText把回车键变成搜索
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/mingyue_1128/article/details/31376159 在xml文件里的EditT ...
- PHP 基础篇 - PHP 中 DES 加解密详解
一.简介 DES 是对称性加密里面常见一种,全称为 Data Encryption Standard,即数据加密标准,是一种使用密钥加密的块算法.密钥长度是64位(bit),超过位数密钥被忽略.所谓对 ...
- SqlAlchemy 中操作数据库时session和scoped_session的区别(源码分析)
原生session: from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine from sqlalch ...
- viewFlipper 之二
main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...
- mybatis架构理解
1. mybatis配置 SqlMapConfig.xml,此文件作为mybatis的全局配置文件,配置了mybatis的运行环境等信息. mapper.xml文件即sql映射文件,文件中配置了操作数 ...
- windows 常用cmd命令
为了减少使用鼠标的频次,熟记一些常用应用的快捷键与系统本身常用的命令是必须的,以下记录一些常用的windows系统命令. 查看网络端口占用情况 :netstat -ano | findstr 8080 ...
- Hbase1.0伪分布式集群启动失败问题
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl/p/7340681.html 转载请注明出处 最近抽空折腾自己的虚拟机环境时启动伪分布式Hbase集群一直失败, ...