[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 ...
随机推荐
- 使用axios实现上传视频进度条
这是最终的效果图 先介绍一下axios的使用:中文的axios官方介绍 首先定义一个uploadTest方法,写在vue文件的methods里 该方法有三个参数,分别是参数,和两个回调函数,参数就是我 ...
- File操作-将txt里的内容写入到数据库表
package com.Cristin.File;//将txt里的内容写入到数据库表 import com.Cristin.MySQL.AddDataToDB;import org.testng.an ...
- Java中的异常 Exceptions
1. 概念 exception是“exceptional event”的缩写,是指执行程序中发生的事件,破坏了程序的正常执行流程.Java 异常处理机制使程序更加健壮易于调试,它可以告诉程序员三个问题 ...
- hadoop中 bin/hadoop fs -ls ls: `.': No such file or directory问题
2.x版本上的使用bin/hadoop fs -ls /就有用 应该使用绝对路径就不会有问题 mkdir也是一样的 原因:-ls默认目录是在hdfs文件系统的/user/用户名(用户名就命令行@符号 ...
- Windows.环境变量(设置)
ZC: 我的示例代码(Delphi):http://www.cnblogs.com/CodeSkill/p/8341464.html 1.资料: 如何用代码设置环境变量?-CSDN论坛.html(ht ...
- libxml2_ZC积累
1.Qt5.3.2(VS2010 OpenGL) 1.1.查找节点的 带NameSpace的属性 参考网址:https://stackoverflow.com/questions/7872413/ho ...
- Codeforces 1043 F - Make It One
F - Make It One 思路: dp + 容斥 首先, 答案不会超过7, 因为前7个质数的乘积大于3e5(最坏的情况是7个数, 每个数都缺少一个不同的因子) 所以从1到7依次考虑 dp[i][ ...
- HRBUST - 2358 Magic network
HRBUST - 2358 思路:dfs序 + 树状数组 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimiz ...
- 零点红旗echarts
当月销量 当月同比 当月市场份额 区域占比 累计销量 累计同比 累计市场份额 区域占比 lists = { district:[ {name: '东北', value: [20100,'+13%',' ...
- lua --- 用break实现continue逻辑
循环中内嵌一个循环,然后将具体的逻辑放在内嵌循环中去处理,在内嵌循环的开始,添加一个判断语句,满足条件就跳出内嵌循环. 示例代码如下: tab = {,,,,} ,#tab do while true ...