AlgoFrames

Pre-order, in-order, post-order

Pre-order, in-order and post-order visit a tree’s nodes in different orders. Watch visit numbers appear while the call stack grows beside the tree.

Before you start: Binary trees and how they are stored, Recursion: base case, recursive case, the call stack, Stacks: push, pop, peek; array and linked implementations.

The idea

A list has a first element. A tree does not, so we need a rule for the visit order.

Three rules cover most tree algorithms: pre-order, in-order and post-order. Each visits every node once. Only the moment of the visit changes.

Complexity

Time is O(n): each node is visited once. Space is O(h): a balanced tree of 7 needs 3 frames, a chain of 5 needs 5.

The stack holds one frame per level. A chain of 100,000 nodes can overflow it, so use an explicit stack when a tree may be that deep.

Pitfalls

Calling dfs on a missing child crashes. Test node.left before the call, or return at the top when node is null.

Variations

Swap the call stack for your own stack to go iterative. Swap it for a queue and you get level order.

Code

Pseudocode

dfs(node)
  visit(node)  # pre-order
  if node.left: dfs(node.left)
  visit(node)  # in-order
  if node.right: dfs(node.right)
  visit(node)  # post-order
  return

Python

def dfs(node, out):
    out.append(node.val)  # pre-order
    if node.left:
        dfs(node.left, out)
    out.append(node.val)  # in-order
    if node.right:
        dfs(node.right, out)
    out.append(node.val)  # post-order
    return

JavaScript

function dfs(node, out) {
  out.push(node.val);  // pre-order
  if (node.left) dfs(node.left, out);
  out.push(node.val);  // in-order
  if (node.right) dfs(node.right, out);
  out.push(node.val);  // post-order
  return;
}

Cost at a glance

TimeO(n)
SpaceO(h)
InputBinary tree
OutputNodes in visit order

Practice problems (9)

Write the three orders Warm-up

Look at the tree. For each traversal, drag the letters into the order the traversal visits them. Left children are always visited before right children.

Match the order to its job Warm-up

Each traversal order fits one job. Match every order to the job it suits best.

In-order of a search tree Warm-up

This binary search tree is traversed in-order. Which sequence does the traversal visit?

Complete the in-order traversal Warm-up

Fill the three blanks so walk collects the values of a binary tree in in-order. Return the values as a list.

Pre-order traversal Standard

Given the root of a binary tree, return the values of its nodes in pre-order: the node, then the left subtree, then the right subtree.

Post-order traversal Standard

Given the root of a binary tree, return the values of its nodes in post-order: the left subtree, then the right subtree, then the node.

In-order iteratively Standard

Given the root of a binary tree, return its values in in-order without recursion. Use an explicit stack.

Fix the post-order traversal Standard

This function should return the values in post-order, but it returns pre-order. Click the line that is wrong.

Build a tree from pre-order and in-order Challenge

Given two lists preorder and inorder that come from the same binary tree with distinct values, rebuild the tree and return its root.

The first pre-order value is the root. Its position in the in-order list splits the other values into the left and right subtree.

Related topics

Next on the path: Level-order traversal