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) ...
随机推荐
- BZOJ3712[PA2014]Fiolki 建图+倍增lca
居然是一道图论题 毫无思路 我们对于每一次的融合操作 $(a,b)$ 建一个新点$c$ 并向$a,b$连边 再将$b$瓶当前的位置赋成$c$ 这样子我们就可以建成一个森林 现在枚举每一种反应$M_i$ ...
- 【NOI2005】聪聪和可可 概率与期望 记忆化搜索
1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1635 Solved: 958[Submit][Statu ...
- hdu 5768 Lucky7 容斥
Lucky7 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- j.u.c系列(04)---之AQS:同步状态的获取与释放
写在前面 在前面提到过,AQS是构建Java同步组件的基础,我们期待它能够成为实现大部分同步需求的基础.AQS的设计模式采用的模板方法模式,子类通过继承的方式,实现它的抽象方法来管理同步状态,对于子类 ...
- Polly简介 — 3. 执行策略
执行策略 执行策略的常见方式是调用策略的Execute函数 var policy = Policy.Handle<TimeoutException>().Retry();policy.Ex ...
- lodoop打印控件详解
注意:使用此打印控件需要引入(在我上传的Demo中都有): install_lodop32.exe install_lodop64.exe LodopFuncs.js jquery-1.10.0.mi ...
- Cygwin、MinGw、mingw-w64,MSys msys2区别与联系
https://www.biaodianfu.com/cygwin-ming-msys.html http://www.mingw-w64.org/doku.php http://blog.csdn. ...
- CSDN学院升级公告
CSDN学院将于2015年8月5日凌晨00:00-10:00进行停站升级,升级期间会影响大家的正常訪问和操作.给各位用户带来的不便敬请谅解. 升级结束后有不论什么问题请发邮件到webmaster@cs ...
- lodash用法系列(2),处理对象
Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能. 官网:https://lodash.com/引用:<script src="//cdnjs.clou ...
- Delphi 实现对注册表的监视和扫描
;iRes := RegEnumKey( hKeyx, dwIndex, buf, dwSize );if iRes = ERROR_NO_MORE_ITEMS thenbreakelse if iR ...