Guest
Sign in to keep your graphs and progress.

Practice book
Graph Theory Through Problems300 Exercises with Hints, Worked Algorithms and Complete Solutions
By Mohammed Islam Hadjoudj
Learn graph theory by solving problems: 300 exercises, from routine checks to challenging proofs, each with a complete solution.
Also by the authorGraph Algorithms for Coding Interviews100 solved interview problems with visual explanations and Python solutions.View on AmazonSelect an algorithm and generate steps to begin visualization
Four ways to walk a tree
Visits every vertex of the tree rooted at the start vertex once: preorder, inorder or postorder with the call stack, or level order with a queue
The start vertex is the root. Children are taken left to right as drawn.
Select an algorithm and generate steps to begin visualization
A tree traversal visits every vertex of a tree exactly once, in an order fixed by the shape of the tree. The three depth-first orders differ only in when a vertex is visited relative to its subtrees: preorder visits the vertex before its subtrees (root, left, right), inorder between its left and right subtree (left, root, right), and postorder after both (left, right, root). Level order, the breadth-first traversal, visits the tree one level at a time from the root down. Traversals are the first algorithms taught on binary trees and the basis of almost every recursive tree algorithm.
The depth-first orders follow one recursive walk: down into the left subtree, back up, down into the right subtree, back up. Preorder outputs a vertex on the way down, inorder between its two subtrees and postorder on the way back up. The recursion keeps the path from the root to the current vertex on the call stack, so the memory it needs is the height of the tree. Level order keeps a queue instead: take the vertex at the front, output it and add its children at the back. Every order visits each vertex once, in O(n) time. In the visualizer the start vertex is the root, children are taken left to right as drawn, and a child drawn left of its parent counts as its left child.
Preorder copies or serializes a tree and prints a file system as a directory listing. Inorder lists the keys of a binary search tree in sorted order, which is the standard way to check a BST. Postorder evaluates expression trees, adds up folder sizes and frees memory, since a node is handled only after everything below it. Level order prints a tree by levels, finds the shallowest node and is the core of breadth-first search. Traversal questions are among the most common in coding interviews.
The three depth-first orders are one recursive walk with the visit placed on a different line. Level order replaces the recursion with a queue.
Preorder(v): Inorder(v): Postorder(v):
visit(v) Inorder(left(v)) Postorder(left(v))
Preorder(left(v)) visit(v) Postorder(right(v))
Preorder(right(v)) Inorder(right(v)) visit(v)
LevelOrder(root):
queue = [root]
while queue is not empty:
v = queue.dequeue()
visit(v)
queue.enqueue(left(v), right(v))Each call does constant work besides its recursive calls and there is one call per vertex, so every order runs in O(n). The recursion is as deep as the tree is tall: about log2(n) for a balanced tree but up to n for a tree that is a single path, which is when an iterative version with an explicit stack is safer.
Traverse the binary search tree from Wikipedia's article on tree traversal, which this page loads, with F at the root.
Example graph: F has children B (left) and G (right). B has A and D, D has C and E, G has only a right child I, and I has a left child H.
The four lists hold the same nine letters in four orders. Preorder or postorder together with inorder determine a binary tree with distinct labels, so the tree could be rebuilt from two of these lists, a classic interview exercise.
Time: O(n) · Space: O(h)
Each vertex is entered and left once, and each edge is followed once down and once up, so all four traversals take O(n) time. The recursive orders keep one stack frame per vertex on the path from the root: O(h) space for a tree of height h, which is O(log n) when the tree is balanced and O(n) when it degenerates into a path. Level order holds at most one level and part of the next in its queue, O(w) for a tree of maximum width w, which reaches n/2 on a complete binary tree. Morris traversal does inorder in O(1) extra space by threading the tree temporarily.
Which order to use depends on whether a vertex needs the results of its subtrees, or they need its own.
| Alternative | Prefer it when | Cost |
|---|---|---|
| Preorder | Handle a node before its children: copy or serialize a tree, print a directory listing, compute depths from the root. | O(n) |
| Inorder | Binary search trees: inorder gives the keys sorted, checks the BST property and finds the k-th smallest key. | O(n) |
| Postorder | A node needs its children's results first: evaluate an expression, add up folder sizes, delete a tree. | O(n) |
| Level order | Work by distance from the root: print by levels, find the shallowest leaf, link the nodes of each level. | O(n) |
| Morris traversal | Inorder or preorder without a stack or recursion when memory is tight, by threading the tree temporarily. | O(n) time, O(1) space |
Related algorithms: Depth-First Search, Breadth-First Search, Prüfer Code, Huffman Coding