Boss lesson · Unit 02
Is it a BST?
Validate with bounds passed down, then find the k-th smallest
What you practise
- Bounds passed down, not just child checks
- K-th smallest
Try a question from this lesson
Pick one
This validator compares every node with its two children. What does it decide about the tree shown, and is that right?
# 8
# / \
# 3 10
# / \ \
# 1 9 14
def is_bst(node):
if node is None:
return True
l, r = node.left, node.right
if l and l.val >= node.val:
return False
if r and r.val <= node.val:
return False
return is_bst(l) and is_bst(r)// 8
// / \
// 3 10
// / \ \
// 1 9 14
function isBst(n) {
if (n === null) return true;
const l = n.left, r = n.right;
if (l && l.val >= n.val) return false;
if (r && r.val <= n.val) return false;
return isBst(l) && isBst(r);
}// 8
// / \
// 3 10
// / \ \
// 1 9 14
function isBst(n: Tree): boolean {
if (n === null) return true;
const l = n.left, r = n.right;
if (l && l.val >= n.val) return false;
if (r && r.val <= n.val) return false;
return isBst(l) && isBst(r);
}Correct answer
It accepts it, but it is not a valid BST
Why
Every parent-child pair passes: 9 is larger than its parent 3. But 9 sits in the left subtree of 8, where everything must be below 8. Checking only children misses violations that span more than one level.
TypeScript noteThe return type boolean is written out because the function returns the result of calling itself. TypeScript cannot infer a return type that depends on itself.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
The boss: pass the bounds down
The task: decide whether a tree is a valid BST.
Give every node a range inherited from its ancestors. Going left lowers the upper bound to the parent's value. Going right raises the lower bound. A node is valid only if its value is strictly inside its range. Start the root with no limits at all.
def valid(node, lo, hi):
if node is None:
return True
v = node.val
if not lo < v < hi:
return False
return (valid(node.left, lo, v)
and valid(node.right, v, hi))function valid(node, lo, hi) {
if (node === null) return true;
const v = node.val;
if (v <= lo || v >= hi) return false;
return valid(node.left, lo, v)
&& valid(node.right, v, hi);
}function valid(
node: Tree,
lo: number,
hi: number,
): boolean {
if (node === null) return true;
const v = node.val;
if (v <= lo || v >= hi) return false;
return valid(node.left, lo, v)
&& valid(node.right, v, hi);
}Python notelo < v < hi is a chained comparison, short for lo < v and v < hi. Start with valid(root, float("-inf"), float("inf")).
JavaScript noteStart with valid(root, -Infinity, Infinity). Both are ordinary numbers that compare as you would hope.
TypeScript note-Infinity and Infinity have type number, so the bounds need no special type. Start with valid(root, -Infinity, Infinity).
9 more exercises in the app
- 3× type the answer
- 1× fill the blanks
- 2× multiple choice
- 1× order the lines
- 1× true or false
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.