In this article
  1. Before you start
  2. Why interviewers ask this
  3. Binary tree from first principles
  4. Worked example
  5. Trade-offs and edge cases
  6. How to answer it in an interview
  7. Common mistakes
  8. Follow-up questions
  9. Sources

Before you start

This page belongs to the coding interview patterns and practice method path, and it assumes you can read short Python.

Why interviewers ask this

Tree problems reward a candidate who sees the shape of a solution before writing it. A candidate who has met tree questions only one at a time relearns the same idea on every question. A candidate who has climbed them in order recognizes it, and spends interview time on edge cases and cost.

The Tech Interview Handbook tree page (last updated August 7, 2026) lists these problems as essential or recommended practice; the easy-to-hard order below is this page's own. A weak answer names a traversal and starts coding. A strong answer says why that traversal fits, tests the empty tree and a skewed tree, and states the cost. A Staff-level answer also says when the tree's height, not its node count, drives the cost.

Binary tree from first principles

Binary tree ladder
A binary tree ladder is this site's own term for an ordered set of binary tree interview problems that runs from easy to hard, so that each rung reuses a skill from the rung before and adds one new idea. It is a practice order, not a standard from a textbook or a company.
Postorder traversal
Postorder traversal is a depth-first walk of a binary tree that visits the left subtree, then the right subtree, then the node itself, so every node is processed after both of its children and can combine their answers.
Height-balanced tree
A height-balanced tree is a binary tree in which, at every node, the heights of the left and right subtrees differ by at most one. The rule applies to every node, not only the root, and it keeps the overall height close to the logarithm of the node count.

In plain words

Picture a company chart. Every manager has at most two direct reports, one on the left and one on the right. The top person is the root. People with no reports are leaves. Everyone except the root has exactly one manager. The analogy breaks in one place: a real chart does not care which report is on the left, but a binary tree does, and many answers depend on it.

A binary tree is a set of nodes in which each node has at most two children (Tech Interview Handbook). A binary search tree (BST) is a binary tree that keeps smaller keys on the left of each node and larger keys on the right. Depth is the number of edges on the path from a node up to the root, per the same page. The height of a tree is the number of edges on the longest path from the root down to a leaf; this page counts edges.

Step 1: Three depth-first orders differ only in when the node is visited

Depth-first search (DFS) follows one branch as deep as it can before backing up. The three depth-first orders differ only in when the node is visited. Preorder traversal is root, left, right. In-order traversal is left, root, right. Postorder is left, right, root (Tech Interview Handbook).

Input: the root of a tree. Output: every value once, in the chosen order. Preorder suits copying or serializing a tree. In-order gives sorted keys on a BST, but on a general binary tree the values are not sorted (codeintuition.io). On the example tree of the worked example below, the root is visited first in preorder, second in in-order and last in postorder.1 You now know the three walks; the next step uses the last one to carry answers upward.

The traversal order is chosen by when to process a nodeTable of four traversals: preorder, in-order, postorder and level order, each with its visit sequence and the kind of problem it suits.OrderVisit sequenceReach for itwhenPreorderroot, left, rightcopy or serializea treeIn-orderleft, root, rightsorted keys of aBSTPostorderleft, right, rootchildren'sanswers firstLevel orderqueue, level byleveloutput groupedby levelDiagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/binary-tree-ladder.html
The traversal order is chosen by when the node must be processed relative to its children.CC BY 4.0 — source: What is a binary tree ladder, and how do you climb it?
Text description of the diagram
  • Preorder visits root, left, right.
  • Level order visits one level at a time using a queue.

Step 2: Postorder answers from the bottom up

Postorder gives each parent its children's answers first. Height, diameter and subtree sum problems all use postorder because the children's answers are needed first (codeintuition.io). This is the idea that makes the middle rungs short.

def max_depth(node):
    if node is None:
        return 0
    return 1 + max(max_depth(node.left), max_depth(node.right))

Input: a node. Output: the number of levels below and including it, so max_depth counts nodes on the longest path, which is the height plus one; it returns 3 for the example tree.1 The empty tree is the base case. Each call waits for both children, then combines: on the example tree the leaves return 1, node 20 returns 2 and the root returns 3. The same shape solves Same Tree (check both children, combine the two results), Invert Binary Tree and Maximum Path Sum (each node passes one value upward). The recursion stack costs space proportional to the height h, and linear space for a skewed tree (AlgoMaster).

Step 3: Level order uses a queue and visits one level at a time

Breadth-first search (BFS) visits all nodes at one distance from the root before the next distance. Level order uses a queue and visits one level at a time, and the Tech Interview Handbook says to use BFS when asked to traverse by level.

from collections import deque

def level_order(root):
    if root is None:
        return []
    result, queue = [], deque([root])
    while queue:
        level = []
        for _ in range(len(queue)):
            node = queue.popleft()
            level.append(node.val)
            if node.left:
                queue.append(node.left)
            if node.right:
                queue.append(node.right)
        result.append(level)
    return result

The key line is len(queue): the nodes in the queue at the start of a round are exactly one level (doocs/leetcode notes). Time and space are both O(n) for n nodes, per the same notes. On the example tree the queue holds [3], then [9, 20], then [15, 7]. A depth-first search that records each node's depth can build the same grouping, so recursion is a choice, not a requirement. The sibling grid BFS and DFS ladder reuses the queue on grids.

Step 4: Height and balance decide the cost

Most BST operations take time directly proportional to the tree's height (Wikipedia), so the height is what you must state. Balanced is a height difference of at most one at every node, which matches the height-balanced definition above (AlgoMaster).

The cost of checking it depends on method. The top-down check, which recomputes heights at every node, is O(n log n) for balanced trees and up to O(n^2) in the worst case for skewed trees; the bottom-up postorder check, where each call returns a height or a failure signal, is O(n) (AlgoMaster). That is step 2 paying off again. A search tree built from sorted keys degenerates into a linked list, so its height is one less than its node count: a chain (Wikipedia, same page). On the example tree, measured in edges, the root's two subtrees have heights 0 and 1 and node 20's have 0 and 0, so the tree is balanced.1

One five-node tree shows each ladder step in turnThe tree with root 3, children 9 and 20, and leaves 15 and 7 under 20. Step one numbers the visits, step two shows heights returned upward, step three the queue rounds, step four the balance check.3pre 1 · in 2 ·post 59pre 2 · in 1 ·post 120pre 3 · in 4 ·post 415pre 4 · in 3 ·post 27pre 5 · in 5 ·post 3returns: 1, 1,1, 2, 3queue: [3][9,20] [15,7]0 vs 1, 0 vs 0:okBalanced: yesDiagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/binary-tree-ladder.html

On one five-node tree, each step adds a skill: visit orders, bottom-up heights, queue rounds, then the balance verdict.CC BY 4.0 — source: What is a binary tree ladder, and how do you climb it?
Step-by-step description
  1. Each node shows its pre, in and post visit number.
  2. Children return heights before the parent combines.
  3. The queue holds one level per round.
  4. Subtree heights differ by at most one.
  • Depth-first orders differ only in when the node is visited.
  • Postorder gives each parent its children's answers first.
  • Level order uses a queue and visits one level at a time.
  • Balance is a height difference of at most one at every node.

The rungs in order

The Tech Interview Handbook labels Maximum Depth, Invert Binary Tree and Lowest Common Ancestor of a BST as essential, and the rest of its list as recommended. The order below is this page's, by difficulty; the problems come from that list. The skill column is advice about what each rung practices, not a published rubric.

Each rung adds one skill to the ones below it, from a single postorder call up to a value passed upward along a path.
RungProblemMain skill practiced
FirstMaximum Depthpostorder with a base case
SecondInvert Binary Treeswap children, recurse on both
ThirdSame Treecompare two trees in lockstep
FourthLevel Order Traversalqueue, one level per round
FifthLowest Common Ancestor of a Binary Treepostorder, combine two answers
SixthRight Side Viewlevel order, keep the last node
SeventhSubtree of Another Treereuse Same Tree inside a walk
EighthConstruct from Preorder and Inordersplit by the root's position
NinthSerialize and Deserializepreorder with null markers
TenthBinary Tree Maximum Path Sumreturn one branch, record the best

The BST rungs are Validate BST, Kth Smallest in a BST and Lowest Common Ancestor of a BST. For the fifth rung, see how to find the lowest common ancestor (LCA). Other ladders, such as the trie ladder and the sorting and binary search ladder, follow the same easy-to-hard idea.

Knowing it is half the job. Saying it is the other half.

Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.

Start Free Trial →

Worked example

Take the first example of LeetCode problem 102: root 3, left child 9, right child 20, and 20 has children 15 and 7 (LeetCode). Level order returns [[3],[9,20],[15,7]] there, which the script reproduces.1 The same five nodes give three depth-first sequences.1

The same five nodes give four different sequences, and only level order groups them by level.
OrderSequence for the tree above
Preorder3, 9, 20, 15, 7
In-order9, 3, 15, 20, 7
Postorder9, 15, 7, 20, 3
Level order[3], [9, 20], [15, 7]

The in-order row is not sorted, because this tree is not a BST.1 In-order alone also cannot identify a tree. A root 1 with a right child 2 and a root 2 with a left child 1 both read 1, 2 in-order, while their preorders are 1, 2 and 2, 1.1 That matches the Handbook's advice that in-order needs a pre-order or post-order partner.

Now the cost. A binary tree of height h holds at most 2^(h+1) − 1 nodes (Wikipedia). Each extra level of height about doubles the maximum node count. Height three holds at most 15 nodes, and height five holds at most 63.1

Each extra level of height doubles a tree's capacityBar chart of the most nodes a binary tree can hold at heights 0 to 5: 1, 3, 7, 15, 31 and 63, so every added level roughly doubles the capacity.Most nodes0204060012345Height (edges)137153163Diagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/binary-tree-ladder.html
Capacity doubles with every level, which is why a balanced tree stays shallow.CC BY 4.0 — source: What is a binary tree ladder, and how do you climb it?
Text description of the diagram
  • Each extra level of height about doubles the maximum node count.
  • Height three holds at most 15 nodes.
A balanced tree of a million nodes is about 19 levels tall, while a chain of the same nodes is almost a million deep.
QuantityValue
Minimum height, 1,000 nodes9
Minimum height, 1,000,000 nodes19
Adelson-Velsky and Landis (AVL) height bound, 1,000,000 nodes (1.44 × 19)about 27 (27.36)
Height of a sorted-insert chain of 1,000,000 nodes999,999 edges

The minimum height with n nodes is log2(n) rounded down, and an Adelson-Velsky and Landis (AVL) tree, which enforces exactly the height-balanced rule defined above, stays within a factor of about 1.44 of that optimum (both from Wikipedia; the AVL bound is in Knuth, 1998, section 6.2.3).1 Heights here count edges, so the chain has one fewer edge than nodes; some sources count nodes instead, so state your convention.

Trade-offs and edge cases

The Handbook's table lists BST operations as O(log n), which assumes a balanced tree; the same page says a very skewed tree is essentially a linked list (Tech Interview Handbook). The honest cost is O(h), and h ranges from about log n to n.

Edge cases to test before you say you are done: the empty tree, a single node, two nodes and a skewed tree (Tech Interview Handbook). Level order on an empty tree returns an empty list (LeetCode).

Recursion is clean but uses stack space proportional to the height. LeetCode 102 allows at most 2,000 nodes (LeetCode). A 2,000-node chain is 2,000 nested calls, which can exceed a language's default recursion limit (CPython's included), so for deep or skewed input use an explicit stack or the queue. The Handbook says to practice writing the traversals iteratively. The at-most-one rule is the AVL condition. Red-black trees use a looser rule that still keeps height logarithmic, and splay trees and treaps do not promise logarithmic height in the worst case (Wikipedia). For ordering problems on graphs rather than trees, see topological sort.

How to answer it in an interview

Interviewer asks

How do you approach a binary tree problem, and how do you pick the traversal?

A strong answer

I choose the traversal by when a node needs its neighbors. Preorder helps me copy or serialize, in-order gives sorted keys only on a search tree, and postorder fits anything where a parent needs its children's answers, like height or path sums. For by-level output I use a queue and process the current queue length each round. Then I test an empty tree, one node and a skewed chain, and I state the cost: linear in the node count, with recursion space proportional to the height.

The spoken structure: name the traversal and why, name the edge cases, then give the cost in terms of nodes and height.

Common mistakes

  • "In-order is always sorted." (wrong) That holds only on a BST. (right) "In-order gives sorted keys because this is a BST; on a general tree it does not."
  • "BST search is O(log n)." (wrong) The cost is the height, and sorted inserts make it linear. (right) "Search is O(h), logarithmic if the tree is balanced."
  • "It is balanced if the root's two sides match." (wrong) The rule applies at every node. (right) "At every node the subtree heights differ by at most one."
  • "I recompute height at each node." (wrong) That is the top-down check, O(n log n) on balanced trees and up to O(n^2) on skewed ones. (right) "I return the height from each call and fail early in one pass."

Follow-up questions

Is in-order traversal always sorted?

No. In-order gives sorted keys only on a binary search tree; on a general binary tree it gives left, root, right but the values are not sorted (codeintuition.io). The worked example above shows one.

Is binary search tree search always O(log n)?

No. Most BST operations take time proportional to the height, and inserting keys in sorted order gives a chain whose height is one less than its node count (Wikipedia). Balanced trees keep the height near log n.

Can one traversal rebuild a binary tree?

Not in-order alone. The Tech Interview Handbook says in-order is insufficient to uniquely serialize a tree and that pre-order or post-order is also required. Preorder with null markers is the usual serialization.

Does level order traversal need recursion?

No. It uses breadth-first search with a queue (doocs/leetcode notes). A depth-first search that tracks depth also works.

What makes a binary tree balanced?

At every node, the left and right subtree heights differ by at most one (AlgoMaster). Equal node counts or equal heights at the root alone are not the definition.

Key takeaways
  • A binary tree ladder is an ordered practice set where each rung adds one skill.
  • Pick the depth-first order by when a node needs its children: postorder for bottom-up answers.
  • Level order is a queue that processes the queue's length each round.
  • Search tree cost is the height, which can be n after sorted inserts.
  • Test the empty tree, one node, two nodes and a skewed chain every time.

Sources

  1. Tree quick reference for coding interviews — Tech Interview Handbook, 7 Aug 2026
  2. Self-balancing binary search tree — Wikipedia, 26 Oct 2026
  3. Binary Tree Level Order Traversal (problem statement) — LeetCode, 26 Oct 2026
  4. Binary Tree Level Order Traversal solution notes — doocs/leetcode (GitHub), 26 Oct 2026
  5. Binary Tree Traversal: Four Orders, One Tree — codeintuition.io, 26 Oct 2026
  6. Balanced Binary Tree — AlgoMaster, 26 Oct 2026
  7. The Art of Computer Programming, Volume 3: Sorting and Searching, section 6.2.3 (balanced trees) — Addison-Wesley (Knuth), 2nd edition, 24 Apr 1998