Question

Consider the following rooted tree with the vertex labeled P as the root:

Rooted tree with root P having left child Q and right child R; Q has child S; R has three children T, U, V; U has child W
Rooted tree with root P

The order in which the nodes are visited during an in-order traversal of the tree is

Choose one option
Official answer and solution
Official answer
A
From
Official GATE 2014 answer key

Solution

Apply the multi-way tree in-order traversal definition: visit the first child's subtree, visit the node, then visit remaining children.

For a general rooted tree, the standard in-order traversal recursively performs:

  1. In-order traversal of the first (leftmost) child's subtree.
  2. Visit the current node.
  3. In-order traversal of the remaining children's subtrees from left to right.

Applying this traversal to the tree rooted at P:

  1. Root P:
    Children of P are Q (first child) and R (second child).

    • Step 1: Traverse subtree at Q.
      • Q has one child S.
      • Traverse subtree at S: visit S.
      • Visit Q.
      • Sequence for Q's subtree: S Q.
    • Step 2: Visit root P.
    • Step 3: Traverse subtree at R.
      • Children of R are T, U, V.
      • Traverse first child T: visit T.
      • Visit node R.
      • Traverse second child U:
        • U has child W: visit W.
        • Visit U.
      • Traverse third child V: visit V.
      • Sequence for R's subtree: T R W U V.
  2. Combine sequences:

    S→Q→P→T→R→W→U→VS \to Q \to P \to T \to R \to W \to U \to V

This gives SQPTRWUV, which corresponds to option A.

Final answer

A

Common trap

Using binary tree in-order intuition on R's three children without properly placing R after its first child T.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 3
Question
22 of 65, printed as CS Q.12
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.