[leetcode]_Validate Binary Search Tree
题目:判断一棵二叉树是否合法。要求二叉树满足 左子树所有值 < 当前值 < 右子树所有值,并且所有点都满足这个条件。
思路:
1、从当前根节点判断,求根节点左子树最大值maxLeft,右子树最小值minRight。
2、判断当前节点值是否满足 maxLeft < current < minRight。
3、如果满足,则递归地判断其左右子树是否同样合法。
代码:
1 public boolean isValidBST(TreeNode root) {
if(root == null) return true;
int maxLeft = maxValue(root.left);
int minRight = minValue(root.right);
return maxLeft < root.val && minRight > root.val && isValidBST(root.left) && isValidBST(root.right);
}
public int maxValue(TreeNode node){
if(node == null) return Integer.MIN_VALUE;
int leftMax = maxValue(node.left);
int rightMax = maxValue(node.right);
return Math.max(node.val , Math.max(leftMax , rightMax));
}
public int minValue(TreeNode node){
if(node == null) return Integer.MAX_VALUE;
int leftMin = minValue(node.left);
int rightMin = minValue(node.right);
return Math.min(node.val , Math.min(leftMin , rightMin));
}
网络上还有一种更简单的解法。今天好累啊,明天再做了。see you~
[leetcode]_Validate Binary Search Tree的更多相关文章
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [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 Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- LeetCode Closest Binary Search Tree Value
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...
- leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)
https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...
- [leetcode]Recover Binary Search Tree @ Python
原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...
随机推荐
- POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】
POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...
- 体系编程、SOC编程那些事儿
转:https://blog.csdn.net/yueqian_scut/article/details/49968897 笔者将从芯片IC的系统设计的角度去诠释如何掌握体系编程和SOC编程.笔者有超 ...
- Appium+eclipse+python环境配置
1.安装安卓开发环境(教程很多,不细写) 2.安装eclipse 下载eclipse,解压即可 3.安装python 下载地址:https://www.python.org/downloads/r ...
- C++二阶构造函数
转自:http://blog.51cto.com/9291927/1896411 一.构造函数的问题 构造函数存在问题: A.构造函数只提供自动初始化成员变量的机会 B.不能保证初始化逻辑一定成功,如 ...
- 关于PropertyGrid控件的排序问题
前些天,由于在项目中需要用到PropertyGrid这个控件,展现其所在控件的某些属性,由于有些控件的属性较多,不易浏览,而且PropertyGrid的排序默认的按照字母的顺序排列的,这样导致在在某些 ...
- dedecms 织梦利用arcpagelist标签实现首页arclist分页
DedeCMS首页arclist分页可以利用arcpagelist标签来实现,这里说一下调用方法:首先必须在首页的<head></head>标签里面引入如下js代码: < ...
- Binder机制-简单用法(一)
Binder算是android里面比较难懂的部分了,但是非常重要,基本上,当我们深入到进程交互的阶段,Binder都是一个绕不开的槛,所以我也希望帮助大家更浅显地了解到这个知识点.笔者想通过3篇博文简 ...
- tech| kafka入门书籍导读
J梳理了一下自己在入门 kafka 时读过的一些书, 希望能帮助到对 kafka 感兴趣的小伙伴. 涉及到的书籍: kafka 权威指南 Kafka: The Definitive Guide (ka ...
- js 小秘密
1.RegExp 对象方法 test检索字符串中指定的值.返回 true 或 false. 支持正则表达式的 String 对象的方法
- python学习笔记(datetime、字符串转换)
datetime对象与字符串可以互相转化 代码如下: from datetime import datetime def datetime_string(time): return time.strf ...