-
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
69 lines (63 loc) · 2.64 KB
/
0098_validate_binary_search_tree.py
File metadata and controls
69 lines (63 loc) · 2.64 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# The smallest value in right subtree should be larger than the node's value --> valild
# The largest value in the left subtree should be smaller than the node's value --> valid
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class DFSSolution:
def isValidBST(self, root: TreeNode) -> bool:
# DFS: put node, right, left in order to a stack
# left subtree's upper bound is the current node's val
# right subtree's lower bound is the current node's val
if root is None:
return True
stack = [(root, float('-inf'), float('inf'))] # node, lower, upper
while stack:
n, lower, upper = stack.pop()
if n is None:
continue
val = n.val
if val >= upper or lower >= val:
return False
stack.append([n.right, val, upper])
stack.append([n.left, lower, val])
return True
class InOrderSolution:
def isValidBST(self, root: TreeNode) -> bool:
# DFS + in-order, will convert a BST to a ascending sorted sequece
# If the sequece is not sorted, the BST is invalid
# Key point for DFS implementation is to push node to the stack until the node's left is None
# Then pop the stack and push the popped node's right to the stack until its left is None
stack, last_element = [], float('-inf')
while stack or root:
while root:
stack.append(root)
root = root.left
node = stack.pop()
if node.val <= last_element:
return False
last_element = node.val
root = node.right
return True
class SingleLoopInOrderSolution:
def isValidBST(self, root: TreeNode) -> bool:
# DFS + in-order, will convert a BST to a ascending sorted sequece
# If the sequece is not sorted, the BST is invalid
# Key point for DFS implementation is to push node to the stack until the node's left is None
# Then pop the stack and push the popped node's right to the stack until its left is None
stack, last_element = [], float('-inf')
current = root
while True:
if current is not None:
stack.append(current)
current = current.left
elif stack:
current = stack.pop()
if current.val <= last_element:
return False
last_element = current.val
current = current.right
else:
break
return True