AlgoFlow
intermediateTrees

Validate a binary search tree

Given a binary tree, return true if it satisfies the BST ordering rule: every node's left subtree contains only smaller values and every right subtree only larger values.

Examples

input 5 / 3 7 / 2 4 6 8

output true

Every node respects the bounds it inherits from its ancestors.

input 5 / 1 4 / · · 3 6

output false

4 is the right child of 5, but 4 < 5 — the ordering rule is already broken.

Hints

Try the problem first. Hints are revealed one at a time so you keep thinking.

Solution

The written approach, complexity analysis and follow-ups stay hidden until you ask for them.