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) ...
随机推荐
- MySQL5.6 GTID、多线程复制
MySQL5.6新特性GTID.多线程复制 在Oracle发布MySQL5.6看到众多新特性之后很兴奋,包括对复制的改进.在MySQL5.5半同步复制之后MySQL5.6又引入GTID.多线程复制,在 ...
- js初学者的div移动
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- java常用注释
@see 加入超链接 @see 类名 @see 完整类名 @see 完整类名#方法名 @version 版本信息 @author 作者信息 @param 参数名 说明 @return 说明 @exce ...
- Result Maps collection does not contain value for java.lang.Integer异常处理
使用Mybatis的时候出现这个问题是因为配置文件的问题造成的,mybatis需要写大量的配置文件, 尽管有mybatis-generator,但是里面的内容有很多还是要自己去写的,在这过程中难免会出 ...
- android 开发中 添加库文件 和so 文件的存放位置和添加依赖
so文件一般存储在 main 当中 jniLibs 当中 然后在build.gradle中添加 sourceSets { main { jniLibs.srcDirs = ['src/main/j ...
- win7下Outlook2010禁止访问具有潜在不安全因素的附件的解决办法
发生情景: 收到.bat .exe等敏感类型附件时,会碰到此问题. 解决方法: 1.打开regedit.exe 2.依次展开HKEY_CURRENT_USER\Software\Microsoft\O ...
- EF快速开发定义数据接口类(转)
using System; using System.Linq; using System.Linq.Expressions; using System.Data.Objects; namespace ...
- OGG for DB2 z/OS 12.2版本发布
2016-04-15 Oracle发布了GoldenGate for DB2 z/OS 12.2.0.1.2.可以从OTN或eDelivery下载,该版本是ogg for DB2 z/OS的第一个1 ...
- Diwali
转帖 今天是印度新年(Diwali), 全公司庆祝,午饭不要钱 一.不到美国不知道,三人行必有我师,二人行必有老印.. 一大早“春眠不觉晓,处处闻老印”:晚上遛个弯“举头望明月,低头见老印”:到山 ...
- Intent之前的对象传递与fragment传递数据
Android中Intent传递类对象提供了两种方式一种是 通过实现Serializable接口传递对象,一种是通过实现Parcelable接口传递对象. 要求被传递的对象必须实现上述2种接口中的一种 ...