Search in a Binary Search Tree — Binary Search Trees

Locate a requested catalogue number in an ordered index and return that entry with everything below it, or nothing if it's absent.

Binary Search Trees · Easy

The problem

A catalogue is stored as a tree keyed by item number: every number in a left branch is smaller than the node above it and every number in a right branch is larger. Find the node whose number equals val and return it. It is displayed as that node with everything below it. If no node has that number, return an empty tree.

The story

A shop keeps its catalogue as a tree ordered by item number: in any branch, smaller numbers sit to the left and larger numbers to the right, and no number repeats. A customer asks for one item number. Find that item and hand it back together with everything stored below it, or an empty result if the shop does not stock it. The catalogue can hold up to 1500 items.

What to return

Constraints

Example

Input
root = [6,3,9,1,4,7,12], val = 3
Output
[3,1,4]

Solve it

More in Binary Search Trees

All algorithm problems