描述

解析

二叉搜索树,其实就是节点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. 理解ffmpeg中的pts,dts,time_base

    首先介绍下概念: PTS:Presentation Time Stamp.PTS主要用于度量解码后的视频帧什么时候被显示出来 DTS:Decode Time Stamp.DTS主要是标识读入内存中的b ...

  2. linux 换源

    Ubuntu换源 ubuntu 的默认源是美国的,所以下载起来特别慢.更换国内源:用vi和gedit 打开 /etc/apt/sources.list 将其中的us.archive 全部替换为 cn. ...

  3. gulp常用方法

    var gulp = require('gulp'); var concat = require('gulp-concat'); //使用gulp-concat合并文件,减少网络请求(静态资源数量): ...

  4. 利用angularjs完成注册表单

    ng-init="username = 'first'"设置初始显示first字段 ng-class="{'error':signUpForm.username.$inv ...

  5. oracle中索引的使用

    声明:以下内容是自己跟着教学视屏学习之后整理而来(主要是自用),如有侵权请告知,将尽快删除. 一.索引 1. 概述:数据库对象之一索引用于提高查询效率索引的内建工作对用户是透明的,由数据库自行维护,我 ...

  6. C/C++.文件是否存在

    1. 2._access, _waccess.html(https://msdn.microsoft.com/en-us/library/1w06ktdy.aspx) int _access( con ...

  7. XML.libXml2_ZC

    1.字符串比较函数: xmlStrcmp(...) 这是大小写敏感的比较 xmlStrcasecmp(...) 这是大小写不敏感的比较(忽略字符串里面字符的大小写) 2.查找节点 2.1.循环 2.2 ...

  8. 后端调用接口在通过webService发布 解决跨域问题

    1.新建一个空的项目 2.添加一个WebService新项   asmx格式的 3.在这里面写方法  加上[WebMethod]标识 前端就可以调用 4.发布WebService  右键服务  添加服 ...

  9. Codeforces 960D - Full Binary Tree Queries

    960D - Full Binary Tree Queries 思路: 用move1[i]记录第i层第1种操作移动的个数(对这一层的个数取模) 用move2[i]记录第i层第2种操作移动的个数(对这一 ...

  10. Mac Python PyQt5 环境搭建

    pip install pyqt5 测试开发环境 在Terminal里敲下以下代码,如果没有报错就说明安装成功了. python -c "import PyQt5" 或是如下图,导 ...