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 ...
随机推荐
- dedecms 5.7文章编辑器附件上传图标不显示
我最近发现在使用dedecms 5.7文章编辑器附件上传图标不显示了,以前是没有问题的,这个更新系统就出来问题了,下面我来给大家分享此问题解决办法. 问题bug:在dedecms 5.7中发现了一 ...
- 微信分享 分享icon和分享标题的简单设置
前几天做的一个活动,用到微信分享功能,分享的icon.分享的标题和内容是自定义的.我上网查了一下,好多是注册微信公众号,使用微信api来实现的,注册微信号比较麻烦,最简单的方法就是 页面的title改 ...
- Unity3D 常用事件
这里总结一下U3D常用的一些事件 //按下事件,GetKeyDown //抬起事件,Input.GetKeyUp //长按事件,Input.GetKey //任意键按下事件,Input.anyKeyD ...
- ContextSwitchDeadlock was detected Message(读取注册表时出现).
google的时候,在StackOverflow中得到个暂时解决的方法: http://stackoverflow.com/questions/2797677/contextswitchdeadloc ...
- IOS开发网络篇之──ASIHTTPRequest详解
目录 目录 发起一个同步请求 创建一个异步请求 队列请求 请求队列上下文 ASINetworkQueues, 它的delegate提供更为丰富的功能 取消异步请求 安全的内存回收建议 向服务器端上传数 ...
- 关于cocoapods和swift中使用oc第三方
mac 系统自带ruby,使用cocoapods,直接安装cocoapods就行 终端:$ sudo gem install cocoapods {安装较慢是因为有墙,查看ruby镜像列表:$ gem ...
- javascript的框架演化
说起javascript不同的人或许有不同的看法,一些资深后台程序员在刚开始的时候根本没有把它当作是一门编程语言,但是随着后面js框架的出现,以及面向对象的程序设计,还有原型,闭包的不断使用,后台程序 ...
- STL库list::sort()实现深度解析
原创,转载请注明出处:STL库list::sort()实现深度解析 list模板的定义以及一些基本成员函数的实现这里我就不赘述了,还不清楚的同学可以到网上查找相关资料或者直接查看侯捷翻译的<ST ...
- Queue学习
Queue在Python中可以算作是一种容器,但是他和list,set,dict不一样. 1. Queue不是Python内置类型.它在Queue模块中定义. 2. 它不是iterator容器,他不能 ...
- ubuntu 14.04.02 LTS 启动项误写入 /dev/sda1 (win 7 loader) 修复
问题描述: 在win7下安装Ubuntu14.04,由于启动项 /boot loader 安装位置错误(/dev/sda1 (win 7 loader) )导致无法进入Windows(在GRUB界面能 ...