Validate Binary Search Tree——LeetCode
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.
题目大意:给定一个二叉树,判断它是不是二叉搜索树,二叉搜索树满足要求左子树所有的值小于当前节点的值,右子树所有的值大于当前节点,左右子树也分别是二叉搜索树。
解题思路:
一、递归验证,每个节点设置一个允许的范围,根节点当然是所有的都可以,其他的节点要大于min并且小于max,并且递归验证左右子树的时候要更新min和max。
10 ----- binary tree (0)
/ \
5 15 -------- binary tree (1)
/ \
6 20
如上,tree(1)是合法的,但是6比10小,6所在的位置应该是位于 10~15之间,所以这棵树是不合法的。也就是说,需要两个值max, min 来记录当前节点应该的取值范围。那么min和max的更新符合什么规则呢?对于左子树,应该小于根节点,对于右子树,应该大于根节点;对于一个节点来说,它的值应该是左子树的上限,右子树的下限。
拿上面的树举例来说,节点15的下限是10,上限为null,递归到左子树节点6,下限为10,上限为15,不符合范围要求,返回false。
public boolean isValidBST(TreeNode root) {
return doValidate(root, null, null);
} private boolean doValidate(TreeNode root, Integer min, Integer max) {
if (root == null) {
return true;
}
int val = root.val;
if (min != null && min >= val) {
return false;
}
if (max != null && max <= val) {
return false;
}
return doValidate(root.left, min, val) && doValidate(root.right, val, max);
}
二、(推荐)直观,清晰的解题思路:二叉搜索树的中序遍历序列应该是递增的,那么只需要检查中序遍历序列是否是递增序的。
public boolean isValidBST2(TreeNode root) {
if (root == null) {
return true;
}
List<Integer> list = new ArrayList<>();
doValidate(root, list);
for (int i = 1; i < list.size(); i++) {
if (list.get(i - 1) >= list.get(i)) {
return false;
}
}
return true;
} void doValidate(TreeNode node, List<Integer> list) {
if (node == null) {
return;
}
doValidate(node.left, list);
list.add(node.val);
doValidate(node.right, list);
}
Validate Binary Search Tree——LeetCode的更多相关文章
- Validate Binary Search Tree [LeetCode]
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Validate Binary Search Tree leetcode java
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 【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
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 【LeetCode】98. Validate Binary Search Tree (2 solutions)
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
随机推荐
- 15、SQL Server 触发器
SQL Server 触发器 触发器是一种特殊的存储过程,只有当试图用数据操作语言DML来修改数据时才会触发,DML包含对视图和表的增.删.改. 触发器分为DML触发器和DDL触发器,其中DML触发器 ...
- Http,Https (SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2 中文帮助文档 分类: ASP.NET 2014-10-28 14:09 177人阅读 评论(1) 收藏
下载地址1:https://securityswitch.googlecode.com/files/SecuritySwitch%20v4.2.0.0%20-%20Binary.zip 下载地址2:h ...
- [原创] SQLite数据库使用清单(上)
1. 介绍 1.1 安装 访问 SQLite 下载页面,从 Windows 区下载预编译的二进制文件. 您需要下载 sqlite-shell-win32-*.zip 和 sqlite-dll-win3 ...
- VS中监视窗口,即时窗口和输出窗口的使用
一.监视窗口 1.配置应用程序,使应用程序处于调试状态. 2.点击“调试”----“窗口”----“监视”----“监视1”,打开监视窗口. 3.在监视窗口中“名称”栏中输入变量名称或html元素id ...
- style currentStyle getComputedStyle的区别和用法
先介绍下层叠样式表的三种形式: 1.内联样式,在html标签中style属性设置. <p style="color:#f90">内联样式</p> 2.嵌入样 ...
- WebService开发步骤
WebService原理什么的百度很多我就说了,无非就是提供一个接口放在服务端,客户端实现这个接口就可以拿到自己需要的东西. 现在简单说一下用myEclipse来实现服务端和客户端.另一种WebSer ...
- Android 新版NDK环境搭建(免Cygwin)
使用最新ndk,直接抛弃cygwin,以前做Android的项目要用到NDK就必须要下载NDK,下载安装Cygwin(模拟Linux环境用的),下载CDT(Eclipse C/C++开发插件),还要配 ...
- 给表格设置border还可以这样玩
<table width="100%" border="0" cellpadding="0" cellspacing="1& ...
- select、poll、epoll用法
我们先从著名的C10K问题开始探讨,由于早期在网络还不普及的时候,互联网的用户并不是很多,一台服务器同时在线100个用户估计在当时已经算是大型应用了.但是随着互联网的发展,用户群体迅速的扩大,每一个用 ...
- WIN7下运行hadoop程序报:Failed to locate the winutils binary in the hadoop binary path
之前在mac上调试hadoop程序(mac之前配置过hadoop环境)一直都是正常的.因为工作需要,需要在windows上先调试该程序,然后再转到linux下.程序运行的过程中,报Failed to ...