Is this a valid binary search tree?

Trees Mid-level Python 3.13+ Kotlin 2.4+ Reviewed By Best Latest Apps

Pick the fix

Which tree passes a parent-and-child-only check but is not a valid search tree?

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Root 5, left child 3, and 3 has a right child 7

Right: 7 > 3 locally, but 7 sits left of 5, which breaks the rule.

A local check never compares 7 with 5; passing the limit 5 down to the left subtree catches it.

Why not the others?
  • B Root 5, left 3, right 8. This one is valid.
  • C Root 5 with only a right child 4. 4 < 5 on the right fails even the local check.

30-second answer

Pass an allowed range down the tree. The root may be anything; a left child must be below its parent and above the parent's own lower limit, and the right side mirrors that. Any node outside its range fails. O(n) time, O(h) stack.

Practise saying it · 60 s. No recording, just a timer

Explain how to validate a binary search tree.

Say your answer out loud, then check which of these points you covered:

  • Pass (low, high) down
  • Local check is not enough
  • O(n), O(h)

Deep dive

Checking only parent and child is the classic wrong answer: a deep left descendant can still be bigger than the root.

Equivalent check: an in-order walk must be strictly increasing.

Use infinities (Python) or Long limits (Kotlin) so Int.MIN_VALUE and Int.MAX_VALUE values work.

Python solution Ran on Python 3.13.5 · mypy --strict
Python 3.13
def is_search_tree(root: Node | None) -> bool:
    def ok(node: Node | None, low: float, high: float) -> bool:
        if node is None:
            return True
        if not low < node.val < high:
            return False
        return (ok(node.left, low, node.val)
                and ok(node.right, node.val, high))
    return ok(root, float("-inf"), float("inf"))

Kotlin Ran on Kotlin 2.4.21 · JDK 21.0.12.1
Kotlin 2.4
fun isSearchTree(root: Node?): Boolean {
    fun ok(node: Node?, low: Long, high: Long): Boolean {
        if (node == null) return true
        if (node.value <= low || node.value >= high) return false
        return ok(node.left, low, node.value.toLong()) &&
            ok(node.right, node.value.toLong(), high)
    }
    return ok(root, Long.MIN_VALUE, Long.MAX_VALUE)
}

Common mistakes

  • Comparing each node only with its direct children.
  • Allowing duplicates without asking whether the tree permits them.

Senior take & follow-ups

Draw the counterexample (a big value deep on the left) before you code. It shows why the range is needed.

Follow-up questions

Think of your answer first, then open the hint.

  1. What if duplicates go to the left?

    Show hint

    Make the left bound inclusive: low < val <= high on that side.

  2. How would you fix a tree where two values were swapped?

    Show hint

    Walk in order, find the two out-of-order spots, swap them back.

What each operation costs: Binary Tree · Binary Search Tree · Tree Traversal

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.