-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0098_Validate_Binary_Search_Tree.py
More file actions
24 lines (21 loc) · 1.03 KB
/
0098_Validate_Binary_Search_Tree.py
File metadata and controls
24 lines (21 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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: Optional[TreeNode]) -> bool:
# Time: O(N)
# Space: O(N)
def recursion(node, min_val=None, max_val=None):
if not node:
return True
if node.left and node.val <= node.left.val or (min_val and min_val >= node.val) or not recursion(node.left, min_val=min_val, max_val=node.val):
return False
if node.right and node.val >= node.right.val or (max_val and max_val <= node.val) or not recursion(node.right, min_val=node.val, max_val=max_val):
return False
return True
return recursion(root)
# Runtime: 56 ms, faster than 76.91% of Python3 online submissions for Validate Binary Search Tree.
# Memory Usage: 16.6 MB, less than 46.02% of Python3 online submissions for Validate Binary Search Tree.