[抄题]:

[思维问题]:

不知道要定义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. javascript创建对象之动态原型模式(五)

    function Human(name, sex) { this.name = name; this.sex = sex; if (typeof this.say != "function& ...

  2. Python自定义状态码枚举类

    在Java里很容易做到自定义有状态码和状态说明的枚举类例如: public enum MyStatus { NOT_FOUND(404, "Required resource is not ...

  3. selenium+python自动化97--unittest参数化(paramunittest)

    前言 paramunittest是unittest实现参数化的一个专门的模块,可以传入多组参数,自动生成多个用例 前面讲数据驱动的时候,用ddt可以解决多组数据传入,自动生成多个测试用例.本篇继续介绍 ...

  4. selenium自动化测试通过localstorage绕过登陆

    引言: 做自动化测试,尤其是通过page object模式做UI自动化测试,登陆是个很麻烦的事情,比如你想对某个页面进行测试,一般直接链接到那个页面是不可能的,总是需要先登陆,然后刷新页面才能到想要的 ...

  5. bootstrap file input 多图片上传编辑THINKPHP5

    {layout name="layout" title="文章添加" /} <form id="defaultForm" role=& ...

  6. Mybatis 为什么不要用二级缓存

    https://www.cnblogs.com/liouwei4083/p/6025929.html mybatis 二级缓存不推荐使用 一 mybatis的缓存使用. 大体就是首先根据你的sqlid ...

  7. easyui datagrid列使用按钮的一些心得(转)

    http://blog.csdn.net/sskicgah/article/details/16939959 以前,用easyui的datagrid,有时候会用到一些操作选项,比如代码如下: $('# ...

  8. SHOW CREATE语句

    show create table tablename 查看某表的建表语句 同理查看存储过程 show create procedure  sp_name

  9. jpa 一对一

    实体   Manager package entity; import javax.persistence.Column; import javax.persistence.Entity; impor ...

  10. 深入浅出理解依赖注入这种由外部负责其依赖需求的行为,我们可以称其为 “控制反转(IoC)”

    原文地址: http://www.insp.top/learn-laravel-container ,转载务必保留来源,谢谢了! 这个组件现在可以很简单的获取到它所需要的服务,服务采用延迟加载的方式, ...