[抄题]:

[思维问题]:

不知道要定义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. 利用百度翻译API,获取翻译结果

    利用百度翻译API,获取翻译结果 translate.py #!/usr/bin/python #-*- coding:utf-8 -*- import sys reload(sys) sys.set ...

  2. [UE4]ChildActor组件

    ChildActor组件可以让一个actor成为另外actor的组成部分,并在视图中展示出来.

  3. [UE4]多播代理

    1. 第一种 DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FReceiveDelegateEvent, FString, Value1, FString, ...

  4. Tcprstat测试mysql响应时间

    Tcprstat测试mysql响应时间 一.tcprstat工具安装与使用 tcprstat 是一个基于 pcap 提取 TCP 应答时间信息的工具,通过监控网络传输来统计分析请求的响应时间. 使用方 ...

  5. nginx配置详解(转)

    Nginx 配置文件详解 user nginx ; #用户 worker_processes 8; #工作进程,根据硬件调整,大于等于cpu核数 error_log logs/nginx_error. ...

  6. 分割List为指定size

    背景 老项目,用的原生的JDBC,获取连接,预编译...然后业务需要要更新很多条数据,我就写了条件为 ——IN()... 根据传入的 list 的 size 循环的给sql语句拼接上“ ? ”为了之后 ...

  7. 代码生成器 CodeSmith 的使用(六)

    在上一篇的版本中,我们生成了数据库所有表中的字段,如果要使数据库中的单个表 生成 PetaPoco 构架下的 ORM 映射,使那怎么办.这是这篇博客的主要内容. 首先来看完整的 Camel 规则模板: ...

  8. 异常处理的设计与重构 pdf

    百度网盘: https://pan.baidu.com/s/1hsQIEGk

  9. 关于VS+ImageWatch在线调试问题

    1.使用VS肯定离不开在线调试 2.使用Opencv在VS下进行图像处理,那肯定少不了Image Watch 这两个软件在线调试都存在大坑,弄得精疲力尽才找到解决办法!!! 以下问题都可以通过这个设置 ...

  10. springboot-dokcer

    项目就一个java文件,仅用于样例 package com.example.demo; import org.springframework.beans.factory.annotation.Valu ...