题目:判断一棵二叉树是否合法。要求二叉树满足 左子树所有值 < 当前值 < 右子树所有值,并且所有点都满足这个条件。

思路:

   1、从当前根节点判断,求根节点左子树最大值maxLeft,右子树最小值minRight。

     2、判断当前节点值是否满足 maxLeft < current < minRight。

3、如果满足,则递归地判断其左右子树是否同样合法。

代码:

 1     public boolean isValidBST(TreeNode root) {
if(root == null) return true; int maxLeft = maxValue(root.left);
int minRight = minValue(root.right); return maxLeft < root.val && minRight > root.val && isValidBST(root.left) && isValidBST(root.right);
} public int maxValue(TreeNode node){
if(node == null) return Integer.MIN_VALUE;
int leftMax = maxValue(node.left);
int rightMax = maxValue(node.right);
return Math.max(node.val , Math.max(leftMax , rightMax));
} public int minValue(TreeNode node){
if(node == null) return Integer.MAX_VALUE;
int leftMin = minValue(node.left);
int rightMin = minValue(node.right);
return Math.min(node.val , Math.min(leftMin , rightMin));
}

网络上还有一种更简单的解法。今天好累啊,明天再做了。see you~

[leetcode]_Validate Binary Search Tree的更多相关文章

  1. LeetCode: Validata Binary Search Tree

    LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...

  2. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  3. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  4. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  5. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  6. LeetCode Closest Binary Search Tree Value II

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...

  7. LeetCode Closest Binary Search Tree Value

    原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...

  8. leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)

    https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...

  9. [leetcode]Recover Binary Search Tree @ Python

    原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/ 题意: Two elements of a binary searc ...

随机推荐

  1. cf100989b

    http://codeforces.com/gym/100989/my B. LCS (B) time limit per test 0.25 seconds memory limit per tes ...

  2. HashMap的简单源码分析(看了大佬的源码,基于1.7) put方法

    参考博客: https://blog.csdn.net/eson_15/article/details/51158865 hashMap中的几个关键属性 //默认初始容量是16,必须是2的幂 stat ...

  3. oracle 11 g数据库卸载(方法二)

    1.开始->设置->控制面板->管理工具->服务 停止所有 Oracle服务. 2.开始->程序->Oracle - OraHome81->Oracle In ...

  4. Servlet+MyBatis项目转Spring Cloud微服务,多数据源配置修改建议

    一.项目需求 在开发过程中,由于技术的不断迭代,为了提高开发效率,需要对原有项目的架构做出相应的调整. 二.存在的问题 为了不影响项目进度,架构调整初期只是把项目做了简单的maven管理,引入spri ...

  5. 手动漏洞挖掘-SQL注入(安全牛笔记)

    substring_index(USER(),"@",l)-- #是将查询出来的结果进行切分,以@符号的方式切分 ’ union select table_name,table_s ...

  6. Spring注解(生命周期)

    对于上面的知识图解,需要一点一点的研究. 首先核心容器: 控制反转 和 依赖注入 创建工程: maven仓库搜索 spring context  : 引入后 <!-- https://mvnre ...

  7. Bellman-Ford算法 - 有向图单源最短路径

    2017-07-27  08:58:08 writer:pprp 参考书目:张新华的<算法竞赛宝典> Bellman-Ford算法是求有向图单源最短路径的,dijkstra算法的条件是图中 ...

  8. Spring与CXF整合

    1.首先引入CXF相关jar包以及spring相关jar包,因项目是maven项目,所以直接在pom.xml文件中引入以下依赖即可(以下只是CXF的依赖包,Spring的也要引入,相关的依赖参考我博客 ...

  9. ExtJS Ext.Ajax.request最好设为同步

    ExtJS 中Ext.Ajax.request最好设为同步,即async: false,因为如果Ajax后面需要用到Ajax更新的数据的话,设置同步,后面才能用到最新的数据. function Get ...

  10. ZC_疑问

    1. 应该可以将所有的 jni需要的函数都放在一个 dll中(Windows下),然后 多个java项目就只要调用一个dll了. 可以测试一下 2.