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.
- 1Start at the root and go down one branch.
- 2Output the node before, between or after its subtrees. That choice sets the traversal.
- 3Return upward when a subtree is finished.
- 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.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
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 visualiserWhat 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.
- 1CodeStory checks your level, from tree basics to interview ready.
- 2CodeStory explains online and campus batches, with timings for your weekdays or weekends.
- 3You decide. No pressure, no payment on the call.