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. UAC在注册表中的对应位置

    UAC在注册表中的对应位置 HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Policies/System 相关键值设置: U ...

  2. Unity3d获取APK签名及公钥的方法

    在Unity3d项目中获取APK包签名公钥的方法,核心思想就是通过JNI调用Android提供的方法.不过Unity3d提供了比JNI更上一层的类AndroidJavaObject以及继承它的Andr ...

  3. 讨论SEO中是锚文本有效,还是纯文本有效呢?

    现在很多SEO好友不断在讨论,在SEO优化中,到底是锚文本有效,还是纯文本有效呢? 在这里给大家举一下列子:如“张家口人才网”这样的就叫做锚文本,意思是在原有的文本中加上超级链接,指向到优化的网页上面 ...

  4. List<T>的对比

    对于类型的对比,linq原来的对比是区分不了的. 对两个list进行查询交集方法.交集,并集的函数是直接用Linq的,这里不再写. List<T> intersectList = quer ...

  5. 让powershell同时只能运行一个脚本(进程互斥例子)

    powershell,mutex,互斥,进程互斥,脚本互斥 powershell脚本互斥例子,在powershell类别文章中,声明原创唯一. powershell 传教士 原创文章 2016-07- ...

  6. CnBlog客户端Windows Live Write安装方法

    官方帮助http://space.cnblogs.com/forum/topic/8550 注:如果自动配置没有成功,需要手动配置: a) 在"Type of  weblog that yo ...

  7. ie7、ie8 下Table 中 td 列固定宽度 未按样式设定显示 曲线解决方案

    <!doctype html> <html> <head> <meta charset='utf-8'> <style> .title {b ...

  8. 通过docker-machine和etcd部署docker swarm集群

    本片文章介绍一下 使用docker-machine 搭建docker swarm 集群:docker swarm是docker 官方搭建的容器集群编排工具:容器编排,就是可以使你像使用一太机器一样来使 ...

  9. .NET微信模拟登录及{base_resp:{ret:-4,err_msg:nvalid referrer}}的解决办法

    12年的时候写了些关于微信开发的内容,当时看好这个东西,可惜当时腾讯开放的权限太少,之后的一年多时间没有太关注了. 现在又要重新开始微信开发的阵容了,微信只是个入口,微网站才是趋势. 我是个水货,所以 ...

  10. Ubuntu 64位下搭建ADT的种种问题

    我使用的adt版本为 adt-bundle-linux-x86_64-20140702.zip 1. Eclipse启动时提示 adb 无法加载动态链接库 libstdc++.so.6 以及  lib ...