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

思路:

   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. HDU - 6336 Problem E. Matrix from Arrays (规律+二维前缀和)

    题意: for (int i = 0; ; ++i) { for (int j = 0; j <= i; ++j) { M[j][i - j] = A[cursor]; cursor = (cu ...

  2. maven入门学习(一)

    一.maven介绍 1.软件开发中我们为什么要使用maven呢?(纯属个人体会观点,如有错误,敬请指正) (1)其一,企业岗位需求,目前的IT开发招聘岗位上,基本都要求会使用maven.        ...

  3. ie9下面的console的bug

    摘自:http://blog.csdn.net/cdnight/article/details/51094464 ie9下面,很奇怪的是有console的代码有时候执行不下去,不过当f12打开控制台的 ...

  4. [ufldl]Supervised Neural Networks

    要实现的部分为:forward prop, softmax函数的cost function,每一层的gradient,以及penalty cost和gradient. forwad prop forw ...

  5. Kali更新源,亲测目前可用的源

    kali更新的时候老是无法定位软件包,网络上大部分中科大.阿里云kali源都不可用,都千篇一律,最后找了这个,网易的,还不错,贴出来大家看看: # 源 deb http://mirrors.163.c ...

  6. bzoj 1623: [Usaco2008 Open]Cow Cars 奶牛飞车

    1623: [Usaco2008 Open]Cow Cars 奶牛飞车 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 325  Solved: 223[S ...

  7. [BZOJ2730]矿场搭建

    Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使得无论哪一 ...

  8. 谈谈对Canal(增量数据订阅与消费)的理解

    概述 canal是阿里巴巴旗下的一款开源项目,纯Java开发.基于数据库增量日志解析,提供增量数据订阅&消费,目前主要支持了mysql(也支持mariaDB). 起源:早期,阿里巴巴B2B公司 ...

  9. resultMap结果集映射

    resultMap结果集是用来定义sql查询的结果与java对象的映射关系.它主要解决2大问题: 1)可以解决POJO属性名和表结构的字段名不一致问题(甚至是 不是标准的驼峰命名法) 2)可以完成高级 ...

  10. static变量的作用

    在C语言中,关键字static的意思是静态的,有3个明显的作用: 1. 在函数体内,静态变量具有记忆作用,即一个被声明为静态的变量在这一函数被调用的过程中其值维持不变. 2. 在模块内(但在函数体外) ...