[LeetCode] 98. Validate Binary Search Tree(是否是二叉搜索树) ☆☆☆
描述
解析
二叉搜索树,其实就是节点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(是否是二叉搜索树) ☆☆☆的更多相关文章
- LeetCode第[98]题(Java):Validate Binary Search Tree(验证二叉搜索树)
题目:验证二叉搜索树 难度:Medium 题目内容: Given a binary tree, determine if it is a valid binary search tree (BST). ...
- 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 ...
- LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
随机推荐
- JavaWeb 基础学习
XMAPP是自己封装的一套 web 开发套件 —— 例如Tomcat等是用自己的,而不是使用系统中其他地方安装好了的.此外将提供的 xampp 工具解压到 D 盘根目录下.(注意 xampp 一定要解 ...
- vue--存储
storage 一个存储库,它支持具有相同 api 的 sessionStorage 和 localStorage 安装和用法: storage 的 API: set(key,val) 用key和va ...
- 设计模式(六)Prototype Pattern 原型模式
通过new产生一个对象非常繁琐,可以使用原型模式 原型模式实现: ——Cloneable接口和clone方法 ——Prototype模式实现起来最困难的地方是实现内存的复制和操作,Java中提供了cl ...
- [原][粒子特效][spark]插值器interpolator
深入浅出spark粒子特效连接:https://www.cnblogs.com/lyggqm/p/9956344.html 插值器是体现粒子生命周期变化的功能 group使用到插值器的方式: 可以看到 ...
- 学习笔记28—Python 不同数据类型取值方法
1.array数据类型 1)-------> y[i,] 或者 y[i] 2.遍历目录下所有文件夹: def eachFile(filepath): pathDir = os.list ...
- MySQL学习(七)
学习子查询 1 查出本网站最新的good_id最大的一条商品(要求取出商品名) mysql> select goos_id,goods_name from goods -> order b ...
- 单细胞数据高级分析之初步降维和聚类 | Dimensionality reduction | Clustering
个人的一些碎碎念: 聚类,直觉就能想到kmeans聚类,另外还有一个hierarchical clustering,但是单细胞里面都用得不多,为什么?印象中只有一个scoring model是用kme ...
- linux文件管理之管道与重定向
============================================================== 内容提要: 输入输出重定向.管道: 重定向的作用: 文件描述符 0 1 2 ...
- You Don't Know JS: Async & Performance(第3章, Promises)(未看)
Chapter 3: Promises But what if we could uninvert that inversion of control? What if instead of hand ...
- linux动态库
1.编写动态库函数接口 gcc -fPIC -shared -o libfunction.so funtion.c 2.写头文件 3.测试 gcc test.c -L. -lfuntion -o ru ...