CCI4.5/LintCode Validate Binary Search Tree
Validate BST是指按中序遍历niorder后左<node<右;
第一种方法: 先按inoreder遍历, 再放进ArrayList里用循环看是不是从小到大排序;
注意: 设置成员变量list时,如果先new作ArrayList, 则在main函数里每次用都得new个新的class对象;
如果不想在main里每次都new, 则在判断valid的函数里再new作ArrayList, 这样就每次用这个函数时都会自己new了;
(因为list特性的add功能会被不停地加, 而不是自身清空再加, 所以每次对不同的新对象时要先new)
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
public List<Integer> list;
public void inorder(TreeNode root){
if(root == null) return; inorder(root.left);
list.add(root.val);
inorder(root.right); } /**
* @param root: The root of binary tree.
* @return: True if the binary tree is BST, or false
*/
public boolean isValidBST(TreeNode root) {
list = new ArrayList<Integer>(); inorder(root);
for(int i = 0; i < list.size()- 1; i++){
if(list.get(i) >= list.get(i+1)) return false;
}
return true;
// write your code here
} }
第二种方法: 用自身递归, 看node节点的范围是不是: 左<node<右, 再对接着的左节点及右节点作为node来判断, 以此进行下去;
注意: 一个参数的函数里用个有三个参数的函数来体现实质是使用有三个参数的函数, 另外再对这有三参数的函数定义;
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution { public boolean isValidBST(TreeNode root) {
return isValidBST(root, null, null);
// write your code here
}
public boolean isValidBST(TreeNode root, Integer min, Integer max){
if(root == null) return true; if((min != null && root.val <= min) || (max != null && root.val >= max)) return false; if(!isValidBST(root.left, min, root.val) || !isValidBST(root.right, root.val, max)) return false; return true;
}
}
CCI4.5/LintCode Validate Binary Search Tree的更多相关文章
- LintCode Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- 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) ...
- 39. Recover Binary Search Tree && Validate Binary Search Tree
Recover Binary Search Tree OJ: https://oj.leetcode.com/problems/recover-binary-search-tree/ Two elem ...
- [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树
4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- leetcode dfs Validate Binary Search Tree
Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...
- LeetCode: Validate Binary Search Tree 解题报告
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
随机推荐
- php导入excel
使用phpexcelreader这个类文件来导入excel具体步骤: 先下载文件,然后引入phpexcelreader:下载地址:http://www.waaqi.com/wp-content/upl ...
- 有关利用python获取网页, 以及KDD近几年论文标题与摘要链接
最近一直在看KDD的论文,不过,由于老师并没有什么合理的方向性,所以考虑把kdd中的大部分内容都利用python将所有标题.摘要获取下来. 还有一个原因在于,看acm上的摘要,都只显示了两行,再看多点 ...
- win7 64 位 tomcat 定时重启脚本
添加进任务计划即可 net stop "scm" [停止服务] rd /s /q D:\tomc ...
- 观点:BPM已经过时了?
在这个信息爆炸的世界,业务转型通常是以数字系统和流程的方式发生. 根据Forrester的报告“软件必须丰富你的品牌”,人们要依靠这些技术实现期望的商业成果,比如品牌联系.客户服务创新.更好的产品和提 ...
- windows8.1下android开发环境搭建(Eclipse+Android sdk+ADT+Genymotion)
一.基本jdk.eclipse环境 二.android sdk 1.下载安装:https://developer.android.com/sdk/installing/index.html?pkg=t ...
- java day2一个模拟双色球的代码
package day2; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt ...
- 关于lambda表达式的一些学习——基于谓词筛选值序列
今天看了一些关于lambda表达式的知识,然后对于Func<T,TResult>泛型委托不太熟悉,便查了查相关资料,又引出来了基于谓词筛选值序列这个对我来说的新鲜知识点,于是去查MSDN, ...
- setProgressBarIndeterminateVisibility(true);
此为在标题栏 上 设置一个loading 圈 实用...
- android 开发中fragment 遇到的问题
http://yifeng.studio/2016/12/15/android-fragment-attentions/ 1.不要轻易在 fragment中引用 getactivity(); 解决方案 ...
- svn回滚版本
1.本地修改了内容,但是没有commit可以通过Revert-来覆盖当前版本. 2.已经commit了.首先第一步,应该知道需要回滚的版本编号, 第二步,选择 updata to revision