/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
// 使用栈数据结构实现中序遍历
Deque<TreeNode> stack = new LinkedList();
// 这里需要是初始值inorder为double最大值的负数,是因为有个用例节点的值是int的最小值,,,太难
double inorder = -Double.MAX_VALUE;
while(!stack.isEmpty() || root != null){
if(root != null){
while(root!= null){
stack.push(root);
root = root.left;
}
}
TreeNode node = stack.pop();
if(node.val <= inorder){
return false;
}
inorder = node.val;
root = node.right;
}
return true;
}
}
98. 验证二叉搜索树(中序遍历)
最新推荐文章于 2025-11-24 15:27:16 发布
577

被折叠的 条评论
为什么被折叠?



