Browse curriculum

Insert into a Binary Search Tree

Insert a new value into a binary search tree by walking down from the root to the empty child where it belongs and attaching it as a leaf.

Problem Understanding

Insert into a Binary Search Tree: given the root of a BST and a value that is not already in it, insert the value and return the root. Any valid BST is accepted.

Examples: [4,2,7,1,3], val 5 → [4,2,7,1,3,5]; [40,20,60,10,30,50,70], val 25 → 25 becomes the left child of 30.

The Intuition: Search Until You Fall Off

Search for the value as if it were already there. The search ends at an empty child, and that empty child is exactly where the value belongs.

  1. Smaller than the node: go left.
  2. Larger: go right.
  3. Empty child in that direction: attach the new node.

It is half of Delete Node in a BST, which runs the same search and then has to repair the tree. The result is still a BST, which Validate Binary Search Tree can confirm.

Interactive Walkthrough

INSERT 5 · OUTLINE = PATH WALKED

START AT ROOT

val

5

depth

0

 

Insert 5: walk down from the root 4 to the empty spot where it belongs

Next

5 > 4: go right to 7

Free preview: the first 3 of 4 steps

BST

val

The node the walk is on is filled, the path behind it outlined, and the new leaf turns green. Try Empty tree for the base case.

Dry Run Table

Input: [40,20,60,10,30,50,70], val = 25.

Node Compare Move
40 25 < 40 left to 20
20 25 > 20 right to 30
30 25 < 30 left is empty: attach

Answer: [40,20,60,10,30,50,70,null,null,25].

The Solution Template

One walk down, one new link.

Insert into a Binary Search Tree Code
1var insertIntoBST = function(root, val) {
2 if (!root) return new TreeNode(val);
3 let node = root;
4 while (true) {
5 if (val < node.val) {
6 if (!node.left) { node.left = new TreeNode(val); break; }
7 node = node.left;
8 } else {
9 if (!node.right) { node.right = new TreeNode(val); break; }
10 node = node.right;
11 }
12 }
13 return root;
14};

Edge Cases & Common Mistakes

  • Empty tree: return the new node.
  • Returning the new node instead of the root: the caller wants the original root back.
  • Moving before checking: check that the child is empty before stepping into it, or the walk falls off with nowhere to attach.
  • Duplicates: the problem guarantees the value is new; with duplicates allowed, pick one side consistently.
  • Skewed trees: h can be n, so the worst case is O(n).

Summary

Walk down comparing with each node until the child in the chosen direction is empty, then attach the new node there and return the root. O(h) time, O(1) space iteratively.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Empty tree

    If the root is null, return a new node holding val.

  2. Walk

    From the root, go left when val is smaller and right when it is larger.

  3. Attach

    When the child in that direction is empty, put the new node there and return the original root.

Frequently asked questions

Why can the new value always go in as a leaf?

Walking down by comparisons visits exactly the nodes whose order constraints the new value has to respect. When the child in the chosen direction is empty, the value is larger than every ancestor it went right at and smaller than every ancestor it went left at, so that empty spot keeps the tree a valid BST. No existing node has to move.


Is the answer unique?

No. LeetCode accepts any valid BST that contains the new value, for example one where the new value becomes the root and the old tree is re-hung under it. The leaf insertion is the simplest because it changes a single link.


Recursive or iterative?

Both work: the recursive version returns a new node at the empty spot and lets each caller re-link its child (root.left = insert(root.left, val)). The iterative walk shown here needs O(1) extra space instead of O(h) call stack.


What if the tree is empty?

Then the new node is the whole tree: return it as the root.

New to the technique behind this problem? Watch it run step by step:

Depth First Search Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description