1 Star 0 Fork 0

amusement1234/LeetCode_Java

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
98.验证二叉搜索树.java 2.81 KB
一键复制 编辑 原始数据 按行查看 历史
/*
* @lc app=leetcode.cn id=98 lang=java
*
* [98] 验证二叉搜索树
*
* https://leetcode-cn.com/problems/validate-binary-search-tree/description/
*
* algorithms
* Medium (27.81%)
* Likes: 358
* Dislikes: 0
* Total Accepted: 56.2K
* Total Submissions: 199.9K
* Testcase Example: '[2,1,3]'
*
* 给定一个二叉树,判断其是否是一个有效的二叉搜索树。
*
* 假设一个二叉搜索树具有如下特征:
*
*
* 节点的左子树只包含小于当前节点的数。
* 节点的右子树只包含大于当前节点的数。
* 所有左子树和右子树自身必须也是二叉搜索树。
*
*
* 示例 1:
*
* 输入:
* ⁠ 2
* ⁠ / \
* ⁠ 1 3
* 输出: true
*
*
* 示例 2:
*
* 输入:
* ⁠ 5
* ⁠ / \
* ⁠ 1 4
* / \
* 3 6
* 输出: false
* 解释: 输入为: [5,1,4,null,null,3,6]。
* 根节点的值为 5 ,但是其右子节点值为 4 。
*
*
*/
// @lc code=start
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
long pre = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
// // 解法1:递归
// return helper(root, null, null);
// // 解法2:获取中序遍历的数据,判断是否是升序
// if (root == null)
// return true;
// inOrder(root);
// for (int i = 1; i < res.size(); i++) {
// if (res.get(i) <= res.get(i - 1)) {
// return false;
// }
// }
// return true;
// // 解法3:中序遍历 迭代
Stack<TreeNode> stack = new Stack<>();
TreeNode curr = root;
TreeNode pre = null;
while (curr != null || !stack.isEmpty()) {
while (curr != null) {
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
// if (pre != null && pre.val >= curr.val)
// return false;
// pre = curr;
// curr = curr.right;
// }
// return true;
}
public boolean helper(TreeNode node, Integer lower, Integer upper) {
if (node == null)
return true;
int thisVal = node.val;
if (lower != null && lower >= thisVal)
return false;
if (upper != null && upper <= thisVal)
return false;
return helper(node.left, lower, thisVal) && helper(node.right, thisVal, upper);
}
List<Integer> res = new ArrayList<>();
private void inOrder(TreeNode root) {
if (root == null)
return;
inOrder(root.left);
res.add(root.val);
inOrder(root.right);
}
}
// @lc code=end
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/amusement1234/LeetCode_Java.git
git@gitee.com:amusement1234/LeetCode_Java.git
amusement1234
LeetCode_Java
LeetCode_Java
master

搜索帮助