LeetCode 98. 验证二叉搜索树

题目描述

🔥 98. 验证二叉搜索树

image-20230305213733014

image-20230305213737260

思路分析

中序遍历为升序

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func isValidBST(root *TreeNode) bool {
	if root == nil {
		return true
	}
	var pre *TreeNode
	cur := root
	var stack []*TreeNode
	for cur != nil || len(stack) > 0 {
		for cur != nil {
			stack = append(stack, cur)
			cur = cur.Left
		}
		node := stack[len(stack)-1]
		stack = stack[:len(stack)-1]
		if pre != nil && node.Val <= pre.Val {
			return false
		}
		pre = node
		cur = node.Right
	}
	return true
}

🍏 点击查看 Java 题解

1
write your code here

相似题目

题目 难度 题解
二叉树的中序遍历 Easy  
二叉搜索树中的众数 Easy  
本文作者:
本文链接: https://hgnulb.github.io/blog/83568790
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处!