[抄题]:

[思维问题]:

不知道要定义resultType, 其实用仔细分析判断条件就行了:是否是bst+最大最小值

类似于平衡二叉树:是否平衡+左右的高度差

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

1-1也不是。所以left.max >= root.val也不行

[画图]:

[一刷]:

  1. 空节点可以认为是平衡二叉树
  2. 只有在root.left非空,并且直接拿left.max比root的取val大时,才能否定
  3. 最后可以直接返回left.min, right.max

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[总结]:

定义结构-helper方法-(边界条件-不符合的情况-符合的情况)-调用helper方法

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

BST中出现最多的元素:中序遍历,然后计数统计

public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if the binary tree is BST, or false
*/
public boolean isValidBST(TreeNode root) {
ResultType r = validateHelper(root);
return r.is_bst;
} private ResultType validateHelper(TreeNode root) {
if (root == null) {
return new ResultType(true, Integer.MIN_VALUE, Integer.MAX_VALUE);
} ResultType left = validateHelper(root.left);
ResultType right = validateHelper(root.right); if (!left.is_bst || !right.is_bst) {
// if is_bst is false then minValue and maxValue are useless
return new ResultType(false, 0, 0);
} if (root.left != null && left.maxValue >= root.val ||
root.right != null && right.minValue <= root.val) {
return new ResultType(false, 0, 0);
} return new ResultType(true,
Math.max(root.val, right.maxValue),
Math.min(root.val, left.minValue));
}
}

也可以不定义resulttype 不用这种奇葩思路。直接在isvalidate函数中定义节点为空或者大小不对就行了

验证二叉查找树 · Validate Binary Search Tree的更多相关文章

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

  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. leetcode dfs Validate Binary Search Tree

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

  4. Validate Binary Search Tree

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

  5. 【leetcode】Validate Binary Search Tree

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

  6. LintCode Validate Binary Search Tree

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

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

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

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

  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. HTTP发包工具 -HTTPie

    原文: https://zm8.sm-tc.cn/?src=l4uLj8XQ0IuekZWWi5bRk5CZi5qN0ZyQktCPkIyL0M6cnMmcx8qdoM7PnMrIyMnI&u ...

  2. eclipse如何调试jar包源码

    转载至:http://blog.csdn.net/niclascage/article/details/47451967 引子 深入学习开源代码很多时候都需要去调试别人的jar包.当然如果你能拿别人的 ...

  3. golang web框架 beego 学习 (二) router and controller

    1 Router和Controller的常用配置 beego.Router("/user/admin", &controllers.UserController{}) // ...

  4. [datatable]借助DataTable的Compute方法

    借助DataTable的Compute方法,DataTable中数据不用事先排好序. 下面代码中的dt是跟前面的是一样的 DataTable dtName = dt.DefaultView.ToTab ...

  5. centos7的web环境安装配置

    1.安装基本东西安装apache   yum install httpd安装mariadb  yum install mariadb mariadb-server安装php yum install p ...

  6. Javascript获取数组中的最大值和最小值的方法汇总

    比较数组中数值的大小是比较常见的操作,下面同本文给大家分享四种放哪广发获取数组中最大值和最小值,对此感兴趣的朋友一起学习吧   比较数组中数值的大小是比较常见的操作,比较大小的方法有多种,比如可以使用 ...

  7. faker之python构造虚拟数据

    python中可以使用faker来制造一些虚拟数据 首选安装faker pip install Faker 老版的叫法是faker-factory,但是已不适用 使用faker.Factory.cre ...

  8. JavaScript中的闭包与匿名函数

    知识内容: 1.预备知识 - 函数表达式 2.匿名函数 3.闭包 一.函数表达式 1.定义函数的两种方式 函数声明: 1 function func(arg0, arg1, arg2){ 2 // 函 ...

  9. spring 注解 @NotBlank and BingResult

    @NotEmpty用在集合类上面 @NotBlank 用在String上面 @NotNull 用在基本类型上 在 user对象中需要

  10. JPQL和SQL的比较

    前言 在JAVA EE中,JPQL是专门为Java 应用程序访问和导航实体实例设计的.Java Presistence Query Language(JPQL),java持久性查询语言.它是JPA规范 ...