描述

解析

二叉搜索树,其实就是节点n的左孩子所在的树,每个节点都小于节点n。

节点n的右孩子所在的树,每个节点都大于节点n。

定义子树的最大最小值

比如:左孩子要小于父节点;左孩子n的右孩子要大于n的父节点。以此类推。

中序遍历

中序遍历时,输出的值,和前一个值比较,如果大,就失败。

代码

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
if (null == root) {
return true;
}
return isValidBSTHelper(root, null, null);
} public boolean isValidBSTHelper(TreeNode root, Integer min, Integer max) {
if (min != null && root.val <= min) {
return false;
}
if (max != null && root.val >= max) {
return false;
}
boolean left = root.left != null ? isValidBSTHelper(root.left, min, root.val) : true;
if (left) {
return root.right != null ? isValidBSTHelper(root.right, root.val, max) : true;
} else {
return false;
}
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
Stack<TreeNode> stack = new Stack<>();
//中序遍历
public boolean isValidBST(TreeNode root) {
if (null == root) {
return true;
}
boolean flag = isValidBST(root.left);
if (!flag) {
return false;
}
TreeNode preNode = null;
if (!stack.isEmpty()) {
preNode = stack.peek();
}
if (null != preNode && root.val <= preNode.val) {
return false;
}
stack.push(root);
flag = isValidBST(root.right);
if (!flag) {
return false;
}
return true;
}
}

当然还可以非递归中序遍历,存储节点的话,可以存2个就行。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null)
return true;
Stack<TreeNode> stack = new Stack<>();
TreeNode pre = null;
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
if (pre != null && root.val <= pre.val)
return false;
pre = root;
root = root.right;
}
return true;
}
}

[LeetCode] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆的更多相关文章

  1. LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)

    题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...

  2. LeetCode OJ:Validate Binary Search Tree(合法的二叉搜索树)

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

  3. LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

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

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

  5. Leetcode 98. 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 98 Validate Binary Search Tree ----- java

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

  7. [leetcode]98. Validate Binary Search Tree验证二叉搜索树

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

  8. LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  9. [leetcode]272. Closest Binary Search Tree Value II二叉搜索树中最近的值2

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

随机推荐

  1. Using keytool to import keystore

    open command line and locate to the location of  keytool.exe. import cert to keystore command: keyto ...

  2. 【Python】【面向对象】

    """# [[面向对象]]#[访问限制]#如果要让内部属性不被外部访问,可加双下划线,编程私有变量.只有内部可以访问,外部不能访问.class Student(objec ...

  3. 在阿里云服务器上搭建xampp遇到的问题

    参考文章:http://blog.csdn.net/hel12he/article/details/49781813 http://www.laozuo.org/8178.html http://bl ...

  4. 字符串函数 mysql 和sqlserver 中对于字符串的常用函数和区别

    1. 对于字符串大小写的统一 mysql和sqlserver中都有同名函数lower()和upper(),但是mysql中还有另外一对函数,达到同样的目的,lcase()和ucase(),也就是英文中 ...

  5. webbench高并发测试

    安装ctags sudo apt-get install ctags 安装webbench 下载webbench http://home.tiscali.cz/~cz210552/distfiles/ ...

  6. Java 8里面lambda的最佳实践

    Java 8已经推出一段时间了,越来越多开发人员选择升级JDK,这条热门动弹里面看出,JDK7最多,其次是6和8,这是好事! 在8 里面Lambda是最火的主题,不仅仅是因为语法的改变,更重要的是带来 ...

  7. centos/redhat 删除虚拟网桥virbr0

    kvm虚拟化环境安装好后,ifconfig会发现多了一个虚拟网卡virbr0. 这是由于安装和启用了libvirt服务后生成的,libvirt在服务器(host)上生成一个  virtual netw ...

  8. discuz的学习和部署

    1.http://jingyan.baidu.com/article/b87fe19eb57ff252183568d9.html 下载好后安装一个mysql, 2.

  9. English Voice of << Last Christmas >>

    Last Christmas填 词:乔治·迈克尔谱 曲:乔治·迈克尔编 曲:乔治·迈克尔歌词:Last Christmas I gave you my heart去年的圣诞节,我把心给了你But th ...

  10. LeetCode--496--下一个更大元素I(java)

    给定两个没有重复元素的数组 nums1和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更大 ...