Lesson 23 — Validate a BST
Validate a BST with allowed ranges
It is not enough that each child is on the correct side of its parent: in a BST every node must fit the rule for all its ancestors. So pass down an allowed range (lo, hi). Going left, the node's value becomes the new upper bound; going right, the new lower bound.
# 5
# / # 3 8
# /
# 4 <- 4 < 8 is fine locally, but 4 < 5 breaks the rule
Why it matters: checking an invariant (a rule that must always hold) is how you catch subtle bugs in data structures before they corrupt data.
Fill in the range check and the two recursive calls. Expected: True, then False.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.
Ready for more? Optional paid resources that pick up where this lesson leaves off.
Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.
Project-based Data Structures courses if you want a structured paid path after the free lessons. Wait for a sale — prices drop often.
Browse on Udemy →Search Amazon for Data Structures books — check the publication year so the examples match current versions.
Search Amazon →