Back
Binary Search Tree
Question 1 of 12
8% Complete
Medium
Validate BST using DFS with min/max bounds
Each node value must be within (lo, hi) exclusive; propagate bounds down.
def
isValidBST
(root):
def
dfs
(node, lo, hi):
if
not
node:
return
True
if
not
(
and
):
return
False
return
dfs
(node.left, lo, node.val)
and
dfs
(node.right, node.val, hi)
return
dfs
(root,
float
(
'-inf'
),
float
(
'inf'
))
Submit All Answers
Need a hint?
Left subtree gets hi=node.val; right gets lo=node.val.