Skip to content
Coding Interviews

Trees and Binary Search Trees Explained

By The EbookWale Team · Updated June 16, 2026 · 4 min read

A tree is a hierarchy of nodes; a binary search tree keeps values ordered for O(log n) search. Here's how trees work, the three traversals, the BST property, and the interview problems they power.

A tree is a hierarchy of nodes connected from a single root, and a binary search tree (BST) is a tree that keeps its values ordered so you can search in O(log n). Trees model anything hierarchical — file systems, the HTML DOM, org charts — and they’re a major coding-interview topic because they reward clean recursion.

Get the vocabulary, the BST property, and the three traversals down, and most tree problems become short recursive functions.

Tree vocabulary

  • Root — the top node, with no parent.
  • Child / parent — nodes directly below / above.
  • Leaf — a node with no children.
  • Height — the longest path from a node down to a leaf.
  • Binary tree — every node has at most two children (left, right).
class TreeNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

The binary search tree property

A BST adds one rule that changes everything: for every node, left subtree < node < right subtree.

        8
       / \
      3   10
     / \    \
    1   6    14

That ordering means searching is like binary search — at each node you go left or right, halving what’s left:

function search(node, target) {
  if (!node) return null;
  if (target === node.value) return node;
  return target < node.value
    ? search(node.left, target)    // go left
    : search(node.right, target);  // go right
}

When the tree is balanced, that’s O(log n) — the whole reason BSTs exist.

8 3 10 1 6 14 smaller ↙ ↘ larger
In a BST, left < node < right — so search halves each step.

The three traversals (depth-first)

Visiting every node has three recursive orders, and which you choose matters:

function inOrder(node) {
  if (!node) return;
  inOrder(node.left);
  visit(node.value);        // left → NODE → right
  inOrder(node.right);
}
TraversalOrderUse
In-orderleft, node, rightvisits a BST in sorted order
Pre-ordernode, left, rightcopy / serialise a tree
Post-orderleft, right, nodedelete / evaluate bottom-up

The killer fact: in-order traversal of a BST yields sorted values. Many “is this a valid BST?” and “kth smallest” problems lean on it.

Level-order traversal (breadth-first)

To visit level by level instead of depth-first, use a queue — this is BFS on a tree:

function levelOrder(root) {
  const queue = [root];
  while (queue.length) {
    const node = queue.shift();
    visit(node.value);
    if (node.left) queue.push(node.left);
    if (node.right) queue.push(node.right);
  }
}

Reach for level-order when the problem mentions levels, depth, or “closest” nodes.

🔑 REMEMBER — Most tree problems are recursion in disguise: solve it for the children, then combine. "Max depth" is 1 + max(depth(left), depth(right)). Trust the recursive call on the subtrees and the solution is usually three lines.

Balanced vs unbalanced

A BST’s O(log n) promise assumes it’s balanced. Insert already-sorted data into a naive BST and every node goes right, producing a straight line — a glorified linked list with O(n) operations.

1
 \
  2
   \
    3      ← inserting 1,2,3 in order: degenerate, O(n)

Production libraries use self-balancing trees (AVL, red-black) that rotate on insertion to stay balanced. You rarely implement these in an interview, but you should know why they exist.

Common interview problems

  • Validate a BST — in-order traversal must be strictly increasing.
  • Maximum depth / height — classic recursion.
  • Lowest common ancestor — walk down comparing values (in a BST) or recurse.
  • Level-order / zigzag traversal — BFS with a queue.
  • Invert a binary tree — swap every node’s children recursively.

Common mistakes

  • Forgetting the base case (if (!node) return) → null errors or infinite recursion. See recursion.
  • Confusing tree height with node count.
  • Assuming a binary tree is a BST — only a BST has the ordering; a general binary tree doesn’t.
  • Validating a BST by only checking immediate children — you must enforce the range constraint down the whole subtree.
⚠️ GOTCHA — To validate a BST, checking left < node < right at each node is not enough — a deep descendant can violate the order. Pass a valid (min, max) range down the recursion, or do an in-order traversal and check it's sorted.

Where this fits

Trees are Phase 2 of the coding interview roadmap, built directly on recursion and leading into graphs (a tree is a special graph).

The traversals, BST operations, and recursive templates are drawn out as diagrams in our job-ready tier — JavaScript in Three Months, Python in Three Months, and Java in Three Months. For self-balancing trees, tries, heaps, and segment trees that appear in senior interviews, the for Staff Engineers tier goes deeper: JavaScript, Python, Java.

Think “solve it for the subtrees, then combine,” and trees become the friendliest recursion practice there is.

Frequently asked questions

What is a binary search tree?

A binary search tree (BST) is a binary tree where, for every node, all values in its left subtree are smaller and all values in its right subtree are larger. This ordering lets you search, insert, and delete in O(log n) time when the tree is balanced, by halving the search space at each step.

What are the three tree traversals?

In-order (left, node, right) visits a BST's values in sorted order; pre-order (node, left, right) is used to copy or serialise a tree; post-order (left, right, node) is used to delete or evaluate a tree bottom-up. All three are naturally recursive.

What is the difference between BFS and DFS on a tree?

Depth-first search (DFS) goes as deep as possible down one branch before backtracking, and is usually written recursively — the three traversals are forms of DFS. Breadth-first search (BFS), or level-order traversal, visits the tree level by level using a queue, which is ideal for shortest-path and level-based problems.

Why can a binary search tree degrade to O(n)?

If you insert already-sorted data into a plain BST, every node goes to one side and the tree becomes a straight line — effectively a linked list — so operations become O(n). Self-balancing trees like AVL and red-black trees rebalance on insertion to guarantee O(log n).