Tree Traversals: Worked Examples and Practice for NSCT

By the NSCT Exam Editorial Team · Updated 2026-11-24 · Facts checked against official sources on 2026-11-24

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

  1. Sketch the tree if it is described in words. Mark each node's left and right child clearly; a missing left child changes everything.
  2. Write the rule at the top of your rough work: Pre = N L R, In = L N R, Post = L R N.
  3. Work subtree by subtree. Write the root's slot, then expand each subtree as its own small traversal. Never jump between subtrees.
  4. For level order, keep a queue. Dequeue a node, write it, enqueue its left then right child.
  5. Check with anchors: preorder starts with the root, postorder ends with it, and the inorder of a BST is sorted.
  6. 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.

Binary tree with root M and the four traversal orders: preorder M F B K H P T W R, inorder B F H K P M T R W, postorder B H P K F R W T M, level order M F T B K W H P R.
The page's example tree with every traversal order, generated by code from the tree itself.
          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.

  1. 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.
  2. 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).
  3. Right part: postorder K J N L, root L. Inorder J K | L | N. Postorder K J means J has right child K.
  4. 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

Common mistakes

Practice set

Try these before opening the answers. None of them repeats the quiz below.

  1. Insert 45, 25, 70, 15, 35, 60, 80, 30, 40, 65 into an empty BST. Give the preorder.
  2. For the same BST, give the postorder.
  3. For the same BST, give the level order.
  4. How many leaves does that BST have, and what is its height in edges?
  5. Insert 8, 3, 10, 1, 6, 14, 4, 7, 13 into an empty BST. Give the postorder.
  6. Write the prefix form of x * (y + z) - u / v using its expression tree.
  7. Write the postfix form of the same expression.
  8. A BST has postorder 5, 15, 10, 25, 35, 30, 20. Give its preorder.
  9. A binary tree has postorder D E B F C A and inorder D B E A C F. Give its preorder.
  10. Which traversal lists the keys of a BST in descending order?
Show answers
  1. 45, 25, 15, 35, 30, 40, 70, 60, 65, 80. Root first, then the whole left subtree.
  2. 15, 30, 40, 35, 25, 65, 60, 80, 70, 45. The root 45 comes last.
  3. 45, 25, 70, 15, 35, 60, 80, 30, 40, 65. Depth by depth, left to right.
  4. 5 leaves (15, 30, 40, 65, 80) and height 3, along the path 45-25-35-30.
  5. 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.
  6. x y z + * u v / -. Postorder of the same tree.
  7. 20, 10, 5, 15, 30, 25, 35. The last postorder key, 20, is the root.
  8. A B D E C F. F is the right child of C, because it follows C in inorder.
  9. Reverse inorder (Right, Root, Left): 80, 70, 65, ... for the BST in question 1.

Practise: 20 questions

Interactive quiz loading. The first 10 questions are shown below.

  1. In a preorder traversal of a binary tree, when is the root of each subtree visited?

    1. Before both of its subtrees
    2. Between its left and right subtrees
    3. After both of its subtrees
    4. Only after every node at its depth
    Show answer

    Answer: A. Before both of its subtrees

    Preorder is Root, Left, Right: each node is output before anything in its subtrees. Inorder puts it between the subtrees, postorder after them, and level order goes depth by depth.

  2. Which node is always the LAST one output by a postorder traversal of a non-empty binary tree?

    1. The rightmost leaf
    2. The root
    3. The leftmost leaf
    4. The deepest node
    Show answer

    Answer: B. The root

    Postorder is Left, Right, Root, so the root of the whole tree is emitted only after both of its subtrees are finished. That makes it the final node. The first node in preorder is also the root.

  3. Which node is the FIRST one output by an inorder traversal of a non-empty binary tree?

    1. The root
    2. The leftmost leaf at the deepest level
    3. The node reached by following left pointers from the root until there is no left child
    4. The left child of the root
    Show answer

    Answer: C. The node reached by following left pointers from the root until there is no left child

    Inorder visits the left subtree before the node, so it keeps going left. The first node printed is the one with no left child at the end of that left chain. It need not be a leaf: it may have a right child.

  4. An expression tree stores operators at internal nodes and operands at leaves. Which traversal produces the expression in prefix (Polish) notation?

    1. Inorder
    2. Postorder
    3. Level order
    4. Preorder
    Show answer

    Answer: D. Preorder

    Prefix notation writes the operator before its operands, which is exactly Root, Left, Right. Postorder gives postfix (reverse Polish) and inorder gives infix without brackets.

  5. An iterative inorder traversal uses an explicit stack instead of recursion. For a binary tree of height h with n nodes, what is the worst-case extra space?

    1. O(1) for any tree
    2. O(log n) for any tree
    3. O(h), which becomes O(n) for a skewed tree
    4. O(n^2) because every node is pushed twice
    Show answer

    Answer: C. O(h), which becomes O(n) for a skewed tree

    The stack holds at most one root-to-node path, so it never holds more than h + 1 nodes. For a balanced tree that is O(log n), but a skewed tree has h = n - 1, giving O(n).

  6. Which traversal is the natural choice for computing the height of every node in a single pass?

    1. Postorder, because a node needs both children's heights first
    2. Preorder, because the root is visited first
    3. Inorder, because it visits nodes in sorted order
    4. Level order, because it visits nodes depth by depth from the root
    Show answer

    Answer: A. Postorder, because a node needs both children's heights first

    height(node) = 1 + max(height(left), height(right)) depends on results from both subtrees, so each node must be processed after its children. That is postorder (Left, Right, Root).

  7. A binary tree has this shape. Root M; M's children are F (left) and T (right); F's children are B (left) and K (right); K's children are H (left) and P (right); T has only a right child W; W has only a left child R. What is its preorder traversal?

    1. M F T B K W H P R
    2. B F H K P M T R W
    3. M F B K T W H P R
    4. M F B K H P T W R
    Show answer

    Answer: D. M F B K H P T W R

    Preorder is Root, Left, Right. Visit M, then all of F's subtree (F, B, K, H, P), then T's subtree (T, W, R): M F B K H P T W R.

  8. Use the same tree. Root M; M's children are F (left) and T (right); F's children are B (left) and K (right); K's children are H (left) and P (right); T has only a right child W; W has only a left child R. What is its inorder traversal?

    1. B F H K P M T W R
    2. B F H K P M T R W
    3. B F K H P M T R W
    4. M F B K H P T W R
    Show answer

    Answer: B. B F H K P M T R W

    Inorder is Left, Root, Right. Left of M gives B F H K P (B, then F, then K's subtree H K P). Then M. On the right, T has no left child, so T comes next, then W's subtree: R before W. Result: B F H K P M T R W.

  9. Use the same tree. Root M; M's children are F (left) and T (right); F's children are B (left) and K (right); K's children are H (left) and P (right); T has only a right child W; W has only a left child R. What is its postorder traversal?

    1. B H P K F W R T M
    2. B H P K F R W T M
    3. R W T P H K B F M
    4. H P B K F R W T M
    Show answer

    Answer: B. B H P K F R W T M

    Postorder is Left, Right, Root. F's subtree gives B H P K F, T's subtree gives R W T, and the root comes last: B H P K F R W T M.

  10. Use the same tree. Root M; M's children are F (left) and T (right); F's children are B (left) and K (right); K's children are H (left) and P (right); T has only a right child W; W has only a left child R. What is its level-order traversal?

    1. M F T B K H P W R
    2. M F B K H P T W R
    3. M T F W K B R P H
    4. M F T B K W H P R
    Show answer

    Answer: D. M F T B K W H P R

    Level order uses a queue and lists depth 0, then 1, then 2, left to right: M | F T | B K W | H P R, giving M F T B K W H P R.

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.