0:00 / 0:00

Is this a valid binary search tree? Checking each parent and child isn't enough; pass down the lowest and highest values each subtree is allowed.

▼

The problem

LeetCode 98, Validate Binary Search Tree (Medium). Given the root of a binary tree, return whether it is a valid binary search tree: every value in a node's left subtree is strictly smaller than the node, every value in its right subtree is strictly larger, and both subtrees are BSTs too.

Example (the walkthrough tree): 5 / 2 8 / 1 4 3 9 (level order).

from math import inf

def is_valid_bst(root):
    def check(node, low, high):
        if not node:
            return True       # empty is fine
        if not low < node.val < high:
            return False      # out of range
        return (check(node.left, low, node.val) and
                check(node.right, node.val, high))
    return check(root, -inf, inf)
TRY IT ON LEETCODE ▶

The solution

from math import inf

def is_valid_bst(root):
    def check(node, low, high):
        if not node:
            return True       # empty is fine
        if not low < node.val < high:
            return False      # out of range
        return (check(node.left, low, node.val) and
                check(node.right, node.val, high))
    return check(root, -inf, inf)

Transcript

Validate Binary Search Tree. Given the root of a binary tree, decide if it's a valid search tree: everything in a node's left subtree is smaller than it, and everything in its right subtree is larger.

Here's our tree. Five at the root, two and eight below it, then one and four under two, three and nine under eight.

The tempting check compares each node with its two children. Two is under five, eight is over it. One and four fit around two, and three and nine fit around eight. Every check passes. However, three sits in five's right subtree, and three is smaller than five. The rule reaches all the way down.

A safe fix compares every node with everything below it. That catches the three, but it repeats work: a tree shaped like a line of a thousand nodes takes about half a million comparisons.

Better: carry the rules down. Give each node an allowed range, low to high. The root allows anything. Going left, the high drops to the parent's value. Going right, the low rises to it. Then each node needs one check: is it strictly inside its range?

Let's walk it. Five, anything goes: pass. Two, under five: pass. One, under two: pass. Four, between two and five: pass. Eight, over five: pass. Three must be between five and eight. It isn't: fail, and we stop. Make it a six, and six passes, then nine, over eight. Every gate opens.

In code, a helper takes a node and its range. An empty node is fine. If the value isn't strictly inside, return false. Otherwise check the left child with the value as its high, and the right child with it as its low. Start with minus infinity to infinity.

Each node is checked once, so the time is O of n. The recursion goes as deep as the tree is tall: O of h space.

Pass the range down, not just the parent. That's Validate Binary Search Tree.