[抄题]:

[思维问题]:

不知道要定义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. Linux设置history命令显示时间

    效果如图: 设置方法如下: vim /etc/bashrc #command-->history set HISTFILESIZE=2000 #保存命令的总数默认总数为1000 HISTSIZE ...

  2. mysql设置索引

    1.添加PRIMARY KEY(主键索引) 语法:ALTER TABLE `表名` ADD PRIMARY KEY ( `列名称` ) mysql>ALTER TABLE `table_name ...

  3. VS:error C3872: '0xe044': this character is not allowed in an identifier解决方法

     从网上粘贴代码到编译器中直接编译的话,会报这个错误,但是代码看上去是没有问题的,实际的原因是因为我们粘贴代码的时候粘贴了中文字符进来. 解决方法:就是把这段代码放到记事本里,选择替换把中文输入空 ...

  4. 6.19-response(响应),session(会话技术,服务器端技术) 内置对象,application(内置对象),pageContext (内置对象),cookie(客户端技术)

    一.response(响应) 页面重定向 response.sendRedirect(""); 转发: request.getRequestDispatcher("&qu ...

  5. XPath 常用语法札记

    * 不包含属性的元素 例如不包含属性的span: span[not(@*)] * 文本包含某部分的元素 例如文本包含Rank的元素: *[contains(text(),'Rank')] * 选择匹配 ...

  6. RESTful API & Swagger入门

    什么是RESTful API? 原文地址:https://blog.csdn.net/hjc1984117/article/details/77334616 Swagger入门教程 https://w ...

  7. 进度条 --- socket ---socketserver

    TCP:可靠的面向连接的协议,传输效率低,全双工通信,流数据传输.运用:web浏览器,电子邮件,文件传输程序 UDP:不可靠的,无连接的服务,传输效率高,面向数据包的传输,只能发短消息.运用:dns ...

  8. 详解Tomcat配置及使用

    2018年06月27日 23:42:34 尘埃丶落定 阅读数:2351    版权声明:本文为博主原创文章,转载请附上作者与出处. https://blog.csdn.net/longyin0528/ ...

  9. Tensorflow线程和队列

    读取数据 小数量数据读取 这仅用于可以完全加载到存储器中的小的数据集有两种方法: 存储在常数中. 存储在变量中,初始化后,永远不要改变它的值. 使用常数更简单一些,但是会使用更多的内存,因为常数会内联 ...

  10. zk-systemd

    [Unit] Description=auto run zk server After=network.target [Service] Type=simple Environment=ZHOME=/ ...