Validate Binary Search Tree leetcode java
题目:
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.
题解:
题目非常善良的给了binary search tree的定义。
这道题就是判断当前树是不是BST,所以递归求解就好。
第一种方法是中序遍历法。
因为如果是BST的话,中序遍历数一定是单调递增的,如果违反了这个规律,就返回false。
代码如下:
1 public boolean isValidBST(TreeNode root) {
2 ArrayList<Integer> pre = new ArrayList<Integer>();
3 pre.add(null);
4 return helper(root, pre);
5 }
6 private boolean helper(TreeNode root, ArrayList<Integer> pre)
7 {
8 if(root == null)
9 return true;
boolean left = helper(root.left,pre);
if(pre.get(pre.size()-1)!=null && root.val<=pre.get(pre.size()-1))
return false;
pre.add(root.val);
boolean right = helper(root.right,pre);
return left && right;
}
第二种方法是直接按照定义递归求解。
“根据题目中的定义来实现,其实就是对于每个结点保存左右界,也就是保证结点满足它的左子树的每个结点比当前结点值小,右子树的每个结点比当前结
点值大。对于根节点不用定位界,所以是无穷小到无穷大,接下来当我们往左边走时,上界就变成当前结点的值,下界不变,而往右边走时,下界则变成当前结点
值,上界不变。如果在递归中遇到结点值超越了自己的上下界,则返回false,否则返回左右子树的结果。”
代码如下:
1 public boolean isValidBST(TreeNode root) {
2 return isBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
3 }
4
5 public boolean isBST(TreeNode node, int low, int high){
6 if(node == null)
7 return true;
8
9 if(low < node.val && node.val < high)
return isBST(node.left, low, node.val) && isBST(node.right, node.val, high);
else
return false;
}
Reference:http://blog.csdn.net/linhuanmars/article/details/23810735
Validate Binary Search Tree leetcode java的更多相关文章
- 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
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Recover Binary Search Tree leetcode java
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- Convert Sorted Array to Binary Search Tree leetcode java
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- Convert Sorted List to Binary Search Tree leetcode java
题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...
- 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练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- OSNIT信息收集分析框架OSRFramework
OSNIT信息收集分析框架OSRFramework OSNIT是一种从公开的信息资源搜集信息的有效方式.Kali Linux集成了一款专用分析工具集OSRFramework.该工具集包含多个常用工具 ...
- BZOJ.2806.[CTSC2012]Cheat(广义后缀自动机 DP 单调队列)
题目链接 首先二分答案L.然后就是判断能否将原串划分出一些长度不小于L的子串,这些子串要是给定n个串中的某个串的子串,且满足它们的长度之和不小于原串长度的90%. 贪心多长选一段什么的显然不对.老老实 ...
- 如何使用windows云服务器搭建IIs、windows服务
如何使用windows云服务器搭建IIs.windows服务,以下针对腾讯云服务器进行说明 1.购买云服务器之后,第1步需要设置的是,找到重装系统.重置密码等处. 2.设置安全组,设置完安全组之后才能 ...
- java使用代理模拟http get请求
直接上代码: import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.InetSocketAd ...
- ARM JTAG 信号 RTCK 应该如何处理?
用户在调试内嵌可综合内核的 CPU 如 ARM7TDMI-S 时,需要通过打开仿真器的自适应时钟功能. 此时,ARM仿真器根据 RTCK 时钟信号的频率,产生可用于 CPU 内核当前时钟主频的最快的 ...
- 打印函数 lodop
Lodop属性和方法详解 例子:LODOP.PRINT_INIT("打印任务名");LODOP.SET_PRINT_COPIES(2);bdhtml=window.document ...
- build-your-own-lisp
https://www.gitbook.com/book/ksco/build-your-own-lisp/details
- Reactor与Proactor比较
from http://www.cnblogs.com/dawen/archive/2011/05/18/2050358.html 1.标准定义 两种I/O多路复用模式:Reactor和Proacto ...
- win8、server 2012 清除winsxs文件夹
使用系统自带的文件清理工具 1.组合键win+x ,选择“命令提示符(管理员)” 复制“dism /online /Cleanup-Image /StartComponentCleanup” 到dos ...
- Opencv2教程一:图像变换之阈值二值threshold
网名:无名 QQ:16349023 email:mengwzy@qq.com 曾经非常少写教程,写的可能有点乱希望大对家有帮助 threshold 方法是通过遍历灰度图中点.将图像信息二值化,处理 ...