Tree Traversals: Worked Examples and Practice for NSCT
A tree traversal question gives you a binary tree (as a picture, a description or two other traversals) and asks for the order in which nodes are visited. There are four orders to master: preorder (Root, Left, Right), inorder (Left, Root, Right), postorder (Left, Right, Root) and level order (depth by depth). These questions matter because Data Structures & Algorithms carries 10% of the NSCT according to HEC's Areas of Competencies, and 2026 candidates averaged only 40.2% accuracy in it, per the HEC NSCT report and APP.
This page deepens the traversal section of Tree Algorithms MCQs. That page covers definitions, BST deletion and AVL trees; here you get longer traces, stack and queue snapshots, and tree reconstruction. Every trace below was run in Python before it was written.
The method, step by step
- Sketch the tree if it is described in words. Mark each node's left and right child clearly; a missing left child changes everything.
- Write the rule at the top of your rough work: Pre = N L R, In = L N R, Post = L R N.
- Work subtree by subtree. Write the root's slot, then expand each subtree as its own small traversal. Never jump between subtrees.
- For level order, keep a queue. Dequeue a node, write it, enqueue its left then right child.
- Check with anchors: preorder starts with the root, postorder ends with it, and the inorder of a BST is sorted.
- Count nodes. Your answer must contain every node exactly once.
Preorder, inorder and postorder on one tree
Use this tree for the next three sections. Root M has children F (left) and T (right). F has children B and K. K has children H and P. T has only a right child W, and W has only a left child R.

M
/ \
F T
/ \ \
B K W
/ \ /
H P R
Worked example. Find all three depth-first traversals.
| Traversal | Rule | Left subtree of M | Root | Right subtree of M | Result |
|---|---|---|---|---|---|
| Preorder | N L R | F B K H P | M first | T W R | M F B K H P T W R |
| Inorder | L N R | B F H K P | M middle | T R W | B F H K P M T R W |
| Postorder | L R N | B H P K F | M last | R W T | B H P K F R W T M |
Two details catch students. T has no left child, so inorder writes T before its right subtree. W has only a left child, so inorder gives R W, while preorder gives W R.
Exam angle: the tree has height 3 (in edges) and 4 leaves (B, H, P, R). Options often differ only in the last two or three nodes, so check the deep end of the tree twice.
Level order with a queue
Level order (breadth-first traversal) visits depth 0, then depth 1 and so on, left to right. It needs a queue, a first-in first-out list.
Worked example. Level order of the same tree. Start with the queue holding M.
| Step | Dequeue and write | Queue after enqueuing its children |
|---|---|---|
| 1 | M | F, T |
| 2 | F | T, B, K |
| 3 | T | B, K, W |
| 4 | B | K, W |
| 5 | K | W, H, P |
| 6 | W | H, P, R |
| 7 | H | P, R |
| 8 | P | R |
| 9 | R | (empty) |
Answer: M F T B K W H P R. Notice that W (depth 2) comes before H and P (depth 3) even though W is on the right.
Iterative inorder with a stack
Recursion uses the call stack. An iterative inorder traversal makes this explicit: keep pushing and moving left; when you cannot go left, pop, print, then move to the right child.
Worked example. Stack contents (bottom to top) right after each node is popped and printed:
| Printed | Stack after the pop |
|---|---|
| B | M, F |
| F | M |
| H | M, K |
| K | M |
| P | M |
| M | (empty) |
| T | (empty) |
| R | W |
| W | (empty) |
The stack never holds more than one root-to-node path, so its size is at most h + 1. That is O(log n) for a balanced tree but O(n) for a skewed one.
Rebuilding a tree from two traversals
Inorder plus either preorder or postorder fixes a binary tree with distinct keys. Preorder plus postorder does not, unless the tree is full.
Worked example. Postorder: B A E C K J N L G. Inorder: A B C E G J K L N. Find the preorder.
- The last postorder node, G, is the root. In the inorder list, A B C E is left of G and J K L N is right.
- Left part: its postorder is B A E C, so C is its root. Inorder A B | C | E: A B goes left, E right. Postorder B A means A is the root of that piece with B as its right child (B comes after A in inorder).
- Right part: postorder K J N L, root L. Inorder J K | L | N. Postorder K J means J has right child K.
- Preorder: G C A B E L J K N.
Exam angle: for a BST you need only one traversal, because inorder is the sorted keys. Preorder 20 10 5 15 30 25 35 rebuilds uniquely by inserting the keys in that order.
Expression trees
An expression tree has operators at internal nodes and operands at leaves. Preorder gives prefix notation, postorder gives postfix, and inorder gives infix without brackets.
Worked example. The expression (p - q) * r + s / t has + at the root, (p - q) * r on the left and s / t on the right.
| Traversal | Output |
|---|---|
| Preorder (prefix) | + * - p q r / s t |
| Postorder (postfix) | p q - r * s t / + |
| Inorder (infix) | p - q * r + s / t |
The inorder line has lost the brackets, so it no longer means the same thing. That is exactly why compilers use postfix; see stack and queue applications for converting infix to postfix directly.
Speed tricks
- Flag method: draw the tree and put a small mark on each node's left side (pre), bottom (in) or right side (post). Trace the outline anticlockwise from the root and write a node when you pass its mark.
- Inorder by projection: for a neatly drawn tree, drop every node straight down onto a line. Read left to right; that is the inorder.
- Mirror check: reversed preorder equals the postorder of the mirror image, not of the original tree.
- First and last: preorder's first node and postorder's last node are both the root. Eliminate options that break this before tracing anything.
Common mistakes
- Treating a lone child as a left child. Read "only a right child" carefully; it moves that node in inorder.
- Writing level order as "preorder with the levels split". Level order finishes a whole depth before going deeper.
- Assuming preorder plus postorder always rebuilds a tree. It fails for a node with one child, because you cannot tell which side it is on.
- Calling the reverse of preorder the postorder. Reversing preorder gives Right, Left, Root order, not Left, Right, Root.
- Forgetting that the first inorder node may have a right child; it is the leftmost node, not necessarily a leaf.
Practice set
Try these before opening the answers. None of them repeats the quiz below.
- Insert 45, 25, 70, 15, 35, 60, 80, 30, 40, 65 into an empty BST. Give the preorder.
- For the same BST, give the postorder.
- For the same BST, give the level order.
- How many leaves does that BST have, and what is its height in edges?
- Insert 8, 3, 10, 1, 6, 14, 4, 7, 13 into an empty BST. Give the postorder.
- Write the prefix form of x * (y + z) - u / v using its expression tree.
- Write the postfix form of the same expression.
- A BST has postorder 5, 15, 10, 25, 35, 30, 20. Give its preorder.
- A binary tree has postorder D E B F C A and inorder D B E A C F. Give its preorder.
- Which traversal lists the keys of a BST in descending order?
Show answers
- 45, 25, 15, 35, 30, 40, 70, 60, 65, 80. Root first, then the whole left subtree.
- 15, 30, 40, 35, 25, 65, 60, 80, 70, 45. The root 45 comes last.
- 45, 25, 70, 15, 35, 60, 80, 30, 40, 65. Depth by depth, left to right.
- 5 leaves (15, 30, 40, 65, 80) and height 3, along the path 45-25-35-30.
- 1, 4, 7, 6, 3, 13, 14, 10, 8. Note 13 is the left child of 14.
-
-
- x + y z / u v. The root is the final minus.
-
- x y z + * u v / -. Postorder of the same tree.
- 20, 10, 5, 15, 30, 25, 35. The last postorder key, 20, is the root.
- A B D E C F. F is the right child of C, because it follows C in inorder.
- Reverse inorder (Right, Root, Left): 80, 70, 65, ... for the BST in question 1.
Practise: 20 questions
FAQs
Which traversal is used most in exams?
Inorder appears most often because it sorts a BST, but postorder and level order are where most marks are lost. Postorder questions usually come with a reconstruction twist, and level-order questions test whether you really use a queue. Practise all four on the same tree until each takes under a minute.
Can I rebuild a tree from preorder and postorder?
Only if the tree is full, meaning every node has zero or two children. If a node has a single child, preorder and postorder look the same whether that child is on the left or the right, so the tree is not unique. With inorder plus either one, the tree is always unique for distinct keys.
Is level order the same as BFS?
Yes. Level-order traversal is breadth-first search applied to a tree. Because a tree has no cycles, you do not need a visited set; the queue alone is enough. On general graphs, BFS needs the visited set, as shown in graph traversal traces.
How much memory does a traversal need?
Recursive and stack-based depth-first traversals need O(h) space for a tree of height h. Level order needs O(w), where w is the widest level, which can reach about n/2 in a complete tree. Morris traversal achieves O(1) extra space by temporarily rewiring pointers, but it is rarely examined.
Next step
Work through the 20 traversal MCQs below, then revisit Tree Algorithms MCQs for BST, AVL and B-tree questions. For the wider picture, go back to the DSA MCQ hub or take a timed NSCT mock test.