描述

解析

二叉搜索树,其实就是节点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. 自定义标签TLD文件中,rtexprvalue子标签的意思

    rtexprvalue的全称是 Run-time Expression Value, 它用于表示是否能够利用JSP表达式. 举例子: 1.定义一个TLD文件: <tag> <name ...

  2. json扩展

    using Newtonsoft.Json.Linq; namespace Utility { public static class JsonExt { /// <summary> // ...

  3. Codeforces 781B. Innokenty and a Football League

    题目链接:http://codeforces.com/contest/781/problem/B 去tmd 2-SAT 直接贪心就可以过去了,优先选择第二种情况. 然而....可以被叉掉(数据水了) ...

  4. 1.spring基础知识讲解

    引言:以下记录一些自己在使用时pringle框架时的一些自己心得合成体会时,如有侵权,请联系本博主. 1. spring基本都使用 spring是一个开源的轻量级框架,其目的是用于简化企业级应用程序开 ...

  5. 搭建Nuget服务器

    1.新建一个web网站应用程序 (最好是ASP.NET空Web应用程序) 2.通过NuGet扩展 引用 NuGet.Server包 引用之后的项目结构为 将此网站部署到IIS上,即可访问,既搭建好了 ...

  6. jmeter线程组之间传参

    背景介绍: 使用jmeter做登录和搜索接口的测试: 登录接口请求头为:Content-Type: application/x-www-form-urlencoded; charset=UTF-8 搜 ...

  7. C/C++.全文件名全路径名分割拆分分解

    1._splitpath ZC:windows api的话 可以使用 PathFindFileNameA.PathFindExtensionA.PathFileExistsA等一系列函数 2.测试代码 ...

  8. VC静态调用DLL(lib)

    1. #pragma comment(lib, "libxml2.lib")#pragma comment(lib, "iconv.lib")#pragma c ...

  9. Java创建WebService

    从java 6之后就提供了简单快速的创建WebService的方法,这里将这种简单的方法记录下来方便以后回看.第一步:首先创建一个java Project,然后创建一个类HelloWorldImpl如 ...

  10. 为 昂达 v891 安装上了 remix OS 了

    起因: 默认的ROM自带一堆垃圾app,最主要的是没有root , 所以卸载不了. 然后试了 Root大师 , 刷机精灵 之类的软件. 我 CTMD , 简直比出厂ROM 还流氓, 不断的强制安装各种 ...