Python coding

Is it a search tree?

Write is_search_tree(root): return True if the binary tree is a valid binary search tree. That means that, for every node, all the values in its left subtree are strictly smaller than the node's value and all the values in its right subtree are strictly larger. Equal values are not allowed, and an empty tree is valid. Each TreeNode has val, left and right.

The tests call the provided run_is_search_tree(values), where values lists the tree level by level with null for a missing child: [1, null, 2, 3] is a root 1 with only a right child 2, whose left child is 3.

  • run_is_search_tree([8,3,10]) → true
  • run_is_search_tree([8,3,10,null,null,2,12]) → false
  • run_is_search_tree([5,5]) → false
Ctrl/⌘ + Enter runs · Tab indents
Hint 1

A node's value is limited by every ancestor, not only by its parent: think of the range of values each subtree is allowed to hold.

Hint 2

Pass an open range (low, high) down the recursion: going left, the node's value becomes the new high; going right, the new low. Start with no limits.

Hint 3

Comparing each node only with its two children fails on [8, 3, 10, null, null, 2, 12]: every parent-child pair looks fine, yet 2 sits to the right of 8. Keep comparisons strict so duplicates fail.

Solution and why it works
pythonO(n) · O(h), h = height (recursion stack)
# --- your code ---
def is_search_tree(root: "TreeNode | None") -> bool:
    def within(node: "TreeNode | None", low: float, high: float) -> bool:
        if node is None:
            return True
        if not low < node.val < high:
            return False
        # going left caps the range at this value; going right raises its floor
        return within(node.left, low, node.val) and within(node.right, node.val, high)

    return within(root, float("-inf"), float("inf"))


# --- provided: do not edit ---
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


def build_tree(values: list[int | None]) -> TreeNode | None:
    # Level order: each existing node takes the next two entries as its left and right child.
    nodes = [None if v is None else TreeNode(v) for v in values]
    children = iter(nodes[1:])
    for node in nodes:
        if node is not None:
            node.left = next(children, None)
            node.right = next(children, None)
    return nodes[0] if nodes else None


def run_is_search_tree(values: list[int | None]) -> bool:
    return is_search_tree(build_tree(values))

The rule is about whole subtrees, so each node must fit a range set by all its ancestors, not just its parent. Carry (low, high) down: going left caps the range at the node's value, going right raises its floor. One visit per node gives O(n). The trap is comparing only parent and child, which accepts [8, 3, 10, null, null, 2, 12]. Keep the comparisons strict to reject duplicates. Equivalent check: the inorder traversal must be strictly increasing.