IAE Academy

GATE 2024 DA Q.28: solution and answer

GATE 2024 · DAQ.28 · 1 mark · MSQ

Consider the following tree traversals on a full binary tree:

(i) Preorder
(ii) Inorder
(iii) Postorder

Which of the following traversal options is/are sufficient to uniquely reconstruct the full binary tree?

  1. (A)

    (i) and (ii)

  2. (B)

    (ii) and (iii)

  3. (C)

    (i) and (iii)

  4. (D)

    (ii) only

See the question in the paper

Answer and solution

ANSWER

(A), (B) and (C): any two of the three traversals suffice to reconstruct a full binary tree, while inorder alone does not.

Official answer: A, B, CSource: Official answer key, GATE 2024 DA (IISc Bengaluru)

  1. Assume, as is standard for reconstruction questions, that every node carries a distinct label. A full binary tree is one in which every node has either or children, so no node has exactly one child.

  2. Preorder + inorder (i, ii): the first preorder element is the root. Its position in the inorder sequence splits the remaining nodes into the left and right subtrees, and the subtree sizes tell us which preorder elements belong to each. Recursing on both parts rebuilds the tree uniquely. This works for any binary tree, so (A) is correct.

  3. Inorder + postorder (ii, iii): the last postorder element is the root. Its position in the inorder sequence splits the nodes into the left and right subtrees, and the left size tells us which postorder elements belong to each. Recursing rebuilds the tree uniquely, for any binary tree. So (B) is correct.

  4. Preorder + postorder (i, iii): for a general binary tree this pair is ambiguous, because a node with exactly one child cannot be classed as a left or a right child. For example, a root with only a left child and a root with only a right child both give preorder and postorder . In a full binary tree no node has exactly one child, so this ambiguity cannot arise.

  5. Reconstruction from this pair: the root is the first preorder element and also the last postorder element. If the tree has more than one node, the root has two children, and the second preorder element is the root of the left subtree. In the postorder sequence, the left subtree occupies the positions up to and including that element, because a subtree's root comes last in its own postorder block. The remaining positions, apart from the final root, form the right subtree. Recurse on each part. Every step is forced, so the tree is unique. So (C) is correct.

  6. Inorder only (ii): it is not sufficient. Take the -node full trees. Tree 1: root , left child with leaves and , right child a leaf . Tree 2: root , left child a leaf , right child with leaves and . Both are full binary trees, and both have inorder sequence , yet they are different trees. So the inorder sequence alone does not determine a full binary tree, and (D) is wrong.

  7. Hence (A), (B) and (C) are each sufficient and (D) is not, so the answer is A, B, C.

KEY IDEA

Inorder together with either preorder or postorder always rebuilds a binary tree. Preorder with postorder fails only because of nodes with a single child, and a full binary tree has none, so it works there too.

WATCH OUT FOR

Remembering the rule that preorder plus postorder cannot rebuild a tree and so rejecting (C). That rule applies to general binary trees, and the question specifies a full binary tree.

From the GATE 2024 DA paper. Every question of this paper · Download the full paper · All 26 Trees & heaps questions, with notes

More Trees & heaps questions