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

思路:

   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. hadoop22---wait,notify

    vv wait和notify,是要加syschronized的,是要获取锁的,wait是释放控制权,别的线程就可以执行了,notify和notifyall是通知其他线程执行.

  2. VMWare 网络连接模式(bridged、NAT、host-only)详解

    转自VMWare 网络连接模式(bridged.NAT.host-only)详解 host-only 在某些特殊的网络调试环境中,如何要求将真实环境和虚拟环境隔离开,这时你就可采用host-only模 ...

  3. #define只有一个参数

    define后面只有一个名字,那么这个宏是空的,不会对代码产生影响. 用来便于阅读的

  4. Parameter Binding in ASP.NET Web API

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding ...

  5. VMware Workstation 12 增加磁盘容量 Windows Server 2012 系统 扩展

    1.安装虚拟机后,检查C盘容量大小,发现C盘现在的空间是59.9GB,如下图: 2.使用window+R键,出现运行窗口,输入‘cmd’——>‘cd C:\Program Files (x86) ...

  6. Substring with Concatenation of All Words, 返回字符串中包含字符串数组所有字符串元素连接而成的字串的位置

    问题描述:给定一个字符数组words,和字符串s,返回字符数组中所有字符元素组成的子串在字符串中的位置,要求所有的字符串数组里的元素只在字符串s中存在一次. 算法分析:这道题和strStr很类似.只不 ...

  7. oracle数据库插入优化

    通过程序要把1000万的数据插入到数据表中,刚开始每100条数据耗时50ms左右,但是越往后越慢,最慢到了十几秒的都有,真实好坑了. 于是在网上百度了一波,如何进行insert优化.倒是有了一点小小的 ...

  8. 使用Mybatis连接数据库时报错:org.apache.ibatis.exceptions.PersistenceException: ### Error updating database.

    我的原因是字段名写错了,去数据库中复制字段名再运行就成功了.

  9. GO学习笔记:函数传值与传指针

    当我们传一个参数值到被调用函数里面时,实际上是传了这个值的一份copy,当在被调用函数中修改参数值的时候,调用函数中相应实参不会发生任何变化,因为数值变化只作用在copy上. 为了验证我们上面的说法, ...

  10. 委托,lambda,匿名方法

    lambda表达式其实就是匿名方法的变体或者说简写. 原来我们用 delegate void Del(int x); Del d = delegate(int x) { return x + 1; } ...