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) ...
随机推荐
- Ubuntu 16.04 安装WPS
1.下载安装包和缺少的字体 安装包下载[http://wps-community.org/download.html?vl=a21#download](这里下载的是最新的安装包) 缺少的字体下载[链接 ...
- QT学习笔记9:QTableWidget的用法总结
最近用QT中表格用的比较多,使用的是QTableWidget这个控件,总结一下QTableWidget的一些相关函数. 1.将表格变为禁止编辑: tableWidget->setEditTrig ...
- BZOJ.1879.[SDOI2009]Bill的挑战(状压DP)
题目链接 f定义和下面的思路一样,转移时枚举填什么字符,去更新f并算出有哪些字符串可以匹配某个状态(见code吧...). 预处理出有哪些字符串在第i位可以转移到某个字符c,dp时&一下状态即 ...
- 【Tsinsen-A1486】树(王康宁) 点分治 + Trie
A1486. 树(王康宁) 时间限制:1.0s 内存限制:512.0MB 总提交次数:455 AC次数:97 平均分:52.62 查看未格式化的试题 提交 试题讨论 试题来源 ...
- Graph database_neo4j 底层存储结构分析(1)
1 neo4j 中节点和关系的物理存储模型 1.1 neo4j存储模型 The node records contain only a pointer to their first pr ...
- Cocos2d-x 3.0游戏开发之虚拟机IOS环境:匹配才是好,莫要随便升级软件
尊重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/34110449 做为一个买不起MAC的Coder,仅 ...
- STM32 USB FS Core and USB OTG Core
STM32 USB-FS-Device development kit Compatible with the STM32F102xx and STM32F103xx series, STM32 L1 ...
- 调试工具BTrace 的使用--例子
http://www.cnblogs.com/serendipity/archive/2012/05/14/2499840.html
- [Asp.net core 2.0]Ueditor 图片上传
摘要 在项目中要用到富文本编辑器,包含上传图片,插入视频等功能.但ueditor只有.net版本,没有支持core.那么上传等接口就需要自己实现了. 一个例子 首先去百度ueditor官网下载简化版的 ...
- 华为机试正式版(西安c/c++/java),今天下午去机试的题目,新奇出炉了!
下面题目都是回顾的.题目都非常easy, 大家有些基础就能够參加!(语言能够是c/c++.也能够是java的) 题目一(60分): 字符串操作. 将小写转换成大写, 将大写转化为小写, 数字的不做转换 ...