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的更多相关文章

  1. LintCode Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  2. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  3. Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  4. 【leetcode】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  5. 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 ...

  6. [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 ...

  7. 【LeetCode练习题】Validate Binary Search Tree

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

  8. leetcode dfs Validate Binary Search Tree

    Validate Binary Search Tree Total Accepted: 23828 Total Submissions: 91943My Submissions Given a bin ...

  9. LeetCode: Validate Binary Search Tree 解题报告

    Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...

随机推荐

  1. C语言经典例题100

    C语言经典例题100 来源 http://www.fishc.com 适合初学者 ----------------------------------------------------------- ...

  2. C#文件流读写文件的简单winform实现

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. Service Broker应用(2):不同server间的数据传输,包含集群

    不同Server之间的数据传输,包含DB使用AlwaysOn 配置脚本: SQL Server Service Broker 跨集群通信 具体的TSQL 脚本语句如下.注意的是TSQL语句是在发送方还 ...

  4. iOS 指定圆角个数

    需要实现的效果很明确,只要左上和右上两个地方圆角,以前都是通过layer 直接设置四个角都变成圆角,然后我就开始了强大的搜索功能 然后我就获得了我想要的东西 技术链接:http://www.xuebu ...

  5. 开源一个网络图片浏览器HooPhotoBrowser

    在公司开发项目中需要弹出展示从网络上下载的图片,并提供滑动展示功能.目前采用同事开发的图片浏览器,后期有时间想优化一下.所以重温了下以前开发的类似微博的项目中的图片浏览器代码,并抽取出来封装成了现在这 ...

  6. java 文件压缩和解压(ZipInputStream, ZipOutputStream)

    最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...

  7. MySQL版本升级之5.6到5.7

    两种升级方式 In-Place Upgrade: Involves shutting down the old MySQL version, replacing the old MySQL binar ...

  8. 【CMD】

    1.dir 2. set (不带参数) 查看环境变量. SET [variable=[string]] variable  指定环境变量名. string    指定要指派给变量的一系列字符串. 3.

  9. 【iCore2 模块相关资料】iM_LAN 100M 以太网模块UDP例程

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  10. Outlook查找未读邮件

    1.查找新邮件的未读邮件,可以在下图中查找 2.恢复已删除邮件,如果邮件是未读邮件,在上图中是查找不到,只能通过视图去查找 步骤2内容摘自百度