Skip to content
Official PYQ · 2004

GATE CS 2004 Q35 — Trees

Explain with AI

Consider the label sequences obtained by the following pairs of traversals on a labeled binary tree. Which of these pairs identify a tree uniquely?

  1. preorder and postorder

  2. inorder and postorder

  3. preorder and inorder

  4. level order and postorder

Choose one option
View worked solution

Answer: B

To uniquely reconstruct a binary tree, we must be able to determine the root of each subtree and partition the remaining nodes into its left and right subtrees.

  • Inorder traversal is crucial because it provides the relative left-to-right order of the nodes. For any root node, all elements appearing before it in the inorder sequence belong to its left subtree, and all elements appearing after it belong to its right subtree.
  • Preorder traversal gives the root of the tree first, while Postorder traversal gives the root of the tree last.

Using these properties:

  1. Inorder and Postorder (II): The last element of the postorder sequence is the root. We locate this root in the inorder sequence to split the tree into left and right subtrees. This process is applied recursively, identifying the tree uniquely.
  2. Preorder and Inorder (III): The first element of the preorder sequence is the root. We locate this root in the inorder sequence to split the tree into left and right subtrees. This also reconstructs the tree uniquely.

Why other pairs fail:

  • Preorder and Postorder (I): If a node has only one child, we cannot determine whether it is a left child or a right child. For example, a tree with root AA and left child BB has preorder A,BA, B and postorder B,AB, A. A tree with root AA and right child BB also has preorder A,BA, B and postorder B,AB, A.
  • Level order and Postorder (IV): Similarly, we cannot uniquely distinguish left and right subtrees. For example, a tree with root AA and left child BB has level order A,BA, B and postorder B,AB, A, which is identical to a tree with root AA and right child BB.

Thus, only pairs II and III can uniquely identify a binary tree.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Trees questions