-
Notifications
You must be signed in to change notification settings - Fork 7
/
avengers.py
31 lines (24 loc) · 1.21 KB
/
avengers.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
"""
create a func
dfs, recursive, create helper func with boundaries that is left and right
inside helper func:
check if node is empty, rem empty node is still binary tree
check if the tree obeys root > left, root < right, if not satisified, not binary return False
recurse, call the func again to chech the left subtree, right subtree, rem "nlr"
check the left subtree, values on the left have to be less than the node val, set the parent to the right boundary,
check the right subtree, values on the right are > than node val, right boundary set to right,
then combine checks set to true, -> true vice versa
return func with root and -inf set to left values can be anything
Time Complexity: O(n)
Space Complexity: O(nm), n-parent nodes, m-child nodes
"""
class Solution:
def isValidBST(self, root):
def checkValid (node, left, right):
if not node:
return True
if not (node.val > left and node.val < right):
return False
return (checkValid (node.left, left, node.val) and
checkValid(node.right, node.val, right))
return checkValid(root, float("-inf"), float("inf"))