forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathayosecu.py
More file actions
47 lines (38 loc) · 1.47 KB
/
ayosecu.py
File metadata and controls
47 lines (38 loc) · 1.47 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from typing import Optional
# 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:
"""
- Time Complexity: O(n), n = The number of nodes
- Space Complexity: O(H), H = The height of tree
- Stack size of checkVal
- H = logn, if the tree is balanced
- H = n, if the tree is skewed
"""
def isValidBST(self, root: Optional[TreeNode]) -> bool:
def checkVal(node, min_val, max_val):
if not node:
return True
if node.val >= max_val or node.val <= min_val:
return False
return checkVal(node.left, min_val, node.val) and checkVal(node.right, node.val, max_val)
return checkVal(root, float("-inf"), float("inf"))
def doTest():
sol = Solution()
root1 = TreeNode(2)
root1.left = TreeNode(1)
root1.right = TreeNode(3)
result1 = sol.isValidBST(root1)
print(f"TC 1 is Passed!" if result1 == True else f"TC 1 is Failed! - Expected: {True}, Result: {result1}")
root2 = TreeNode(5)
root2.left = TreeNode(1)
root2.right = TreeNode(4)
root2.right.left = TreeNode(3)
root2.right.right = TreeNode(6)
result2 = sol.isValidBST(root2)
print(f"TC 2 is Passed!" if result2 == False else f"TC 2 is Failed! - Expected: {False}, Result: {result2}")
doTest()