Step 1 of 4
BSTNode & Ordered Insertion
Start by implementing insert(val) to place nodes according to the BST invariant.
- If the tree is empty (
this.root === null), setthis.root = new BSTNode(val). - Otherwise, traverse down from root:
- If
val === curr.value, ignore duplicate values and return. - If
val < curr.value, branch left. If!curr.left, attach the new node. - If
val > curr.value, branch right. If!curr.right, attach the new node.
- If
- Increment
this.countwhen a node is added.
Your build
Running is free — Submit is what records the step. Or press ⌘↩
Tests
1 case| call | type | expected | result |
|---|---|---|---|
| runOps([["insert",10],["insert",5],["insert",15],["insert",5],["size"],["search",5],["search",99]]) | inserts values into BST without duplicates | [null,null,null,null,3,true,false] | — |
Hints
Stuck? Hints open one at a time, each giving a little more away.
2 hints left