Skip to content

Tree Traversals From Preorder to Level Order
Step by Step

Preorder, inorder, postorder and level order all visit every node once. Only the moment you list a node changes.

Step through every visit in both directions, then share a link to the step where the order surprised you.

What You Watch While the Walk Moves

The walk, the output order and the queue change on every step.

Preorder lists a node before its subtrees, inorder lists it between them and postorder lists it after. Level order uses a queue instead.

CodeStory's visualiser shows the walk through the tree and the order nodes come out in. Marked code lines follow every move, and you can step back, step forward or play.

Load Inorder of a BST first to see a sorted list. Then try the skewed tree to see the call stack grow.

How Tree Traversals Visit Every Node

Steps below follow the walk on your screen.

  1. 1Start at the root and go down one branch.
  2. 2Output the node before, between or after its subtrees. That choice sets the traversal.
  3. 3Return upward when a subtree is finished.
  4. 4Visit every node once, which costs O(n).

Tree Traversal Inputs Worth Trying

Seven presets cover every order and two edge cases.

  • Inorder of a BSTComes out sorted as 1, 3, 4, 6, 7, 8, 10.
  • Preorder: root firstThe root comes first, so the tree can be rebuilt from this list.
  • Postorder: children firstThe root comes last, after every child.
  • Level order: by depthWatch the queue move from depth 0 to depth 3.
  • Skewed tree, inorderWith no left children, inorder and preorder give the same list while the call stack grows to 6.
  • Only left children, postorderWith no right children, postorder equals inorder. Can you see why?
  • A single nodeOne node, one output and an empty queue again.

Tree Traversal Cost in Time and Space

Every node is visited once, so the walk costs O(n) time and O(h) space.

Time
O(n)counted from the nodes you step through
Space
O(h)extra memory for the call stack, which grows with tree height
Pattern
TreesEvery node starts a smaller tree.

Practise Trees Beyond Traversals

Watching shows the order and solving trains you to choose it.

Everything in the App in One Place

Practice, interviews and tools all start free. Swipe to see the rest.

Sign up for 100 AI credits now and get 100 more every month. Start free in the app

Tree Traversals Visualiser FAQs

Open the visualiser and follow the walk node by node.

Open the visualiser
What does the tree traversals visualiser show?

CodeStory's visualiser walks a tree in preorder, inorder, postorder or level order and shows the output order beside the highlighted code. Move back and forth through every step, or let it play.

What is the time complexity of tree traversals?

O(n) time and O(h) space, where h is the tree height. Live operation counts show the work growing with every node you visit.

What is the difference between preorder, inorder and postorder?

Preorder lists a node before its subtrees, inorder lists it between them and postorder lists it after them. Inorder on a BST comes out sorted.

Can I use my own input?

Yes. Enter your own input and the visualiser plays the algorithm on it, including the edge cases that break a solution in an interview.

Is the visualiser free?

Yes. Every new account starts with 100 AI credits and gets 100 more monthly, while watching an algorithm costs nothing.

Stuck on Trees? Get a Free Callback

Tell CodeStory which tree step loses you, and a mentor plans what comes next.

  1. 1CodeStory checks your level, from tree basics to interview ready.
  2. 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
  3. 3You decide. No pressure, no payment on the call.

Get a free callback

I am a
I want to learn

Your number is only used for a CodeStory callback.