[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 ...
随机推荐
- iOS 多线程安全 与 可变字典
这周最大的收获是稍稍通透了 多线程安全字典的重要性. 诱因是,发现了有字典坏地址错误 果断以为是 value 或者 key 是可能出现了空值,补充了潜在的判断,虽然有的位置已经预判断的,但 ...
- h5打开App的方法。
在浏览器中: 法1: location.href = `${scheme}`;//location跳转App是几乎所以情况都支持的. 法2: var ifr = document.createElem ...
- Win32 API编程:网络编程在设置WSAAsyncSelect模型后connect的返回值问题
通过WSAAsyncSelect()可以设置非阻塞异步套接字 ::WSAAsyncSelect(s, hDlg, WM_SOCKET, FD_CONNECT | FD_CLOSE | FD_WRITE ...
- kafka入门使用
kafka版本0.11.0.1以上自带zookeeper,必须要求环境中有jdk,解压后进入目录 1.在kafka解压目录下下有一个config的文件夹,里面放置的是我们的配置文件 consumer. ...
- 【bzoj3170】[Tjoi2013]松鼠聚会(数学题)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3170 这道题要在n个点中求一个点使其他点到该点的切比雪夫距离最小. 有个结论:$ (x ...
- spark 写hbase
部分情况下: saveAsNewAPIHadoopDataset不能用 大坑, org.apache.hadoop.mapred 和 org.apache.hadoop.mapreduce两个包的混乱 ...
- Linux统计文件数目
统计当前目录下文件数目 $ find . -type f | wc -l 统计文件行数 $ wc -l filename 仅统计内容为pattern的行(只有pattern) $ grep -w &q ...
- 利用OPENSSH自身记录密码
大家都知道,OPENSSH是基于Linux下,一款开源,安全性不错的Linux SSH会话连接工具. 在渗透当中,当我们get root了.我们如何来记录Linux管理员登陆过的SSH? 想法如下: ...
- Android中获取资源的id和url方法总结
一,获取android工程里面的各种资源的id; 1.1 string型 比如下面: << string name=”OK”>> 客户端请求成功 << / stri ...
- oracle,查看锁表
(1)锁表查询的代码有以下的形式:select count(*) from v$locked_object;select * from v$locked_object;(2)查看哪个表被锁select ...