-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path98.验证二叉搜索树.py
34 lines (27 loc) · 880 Bytes
/
98.验证二叉搜索树.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#
# @lc app=LeetCode.cn id=98 lang=python3
#
# [98] 验证二叉搜索树
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isValidBST(self, root: TreeNode) -> bool:
def helper(root, min_root, max_root):
if root == None:
return True
if min_root != None and root.val <= min_root.val:
return False
if max_root != None and root.val >= max_root.val:
return False
l = helper(root.left, min_root, root)
r = helper(root.right, root, max_root)
return l and r
res = helper(root, None, None)
return res
# @lc code=end