Step 2 of 4

Binary Search Lookup

You start from the build so far — your own work where you have written it, the reference build where you have not. Either way this step stands on its own.

Implement search(val) to locate values in $O(\log n)$ average time.

  • Starting at root:
    • If node is null, return false.
    • If val === node.value, return true.
    • If val < node.value, continue search in left subtree.
    • If val > node.value, continue search in right subtree.

Your build

Running is free — Submit is what records the step. Or press ⌘↩

Tests

1 case
calltypeexpectedresult
runOps([["insert",50],["insert",30],["insert",70],["insert",20],["insert",40],["search",40],["search",70],["search",25]])finds existing and absent values across tree[null,null,null,null,null,true,true,false]

Hints

Stuck? Hints open one at a time, each giving a little more away.

2 hints left