GATE / CS ARCHIVE

GATE CSE 1998 / QUESTION 20

Reconstruct traversal tree

Data Structures · Tree Properties & Traversals · Full GATE CSE 1998 paper

GATE 1998, Q20 5 marks · Descriptive
Report issue

Tree Properties & Traversals

Question 20, GATE 1998: Construct the tree with inorder a f b c d g e and postorder a f c g e d b.
Question 20, GATE 1998: Construct the tree with inorder a f b c d g e and postorder a f c g e d b.
Tap to enlarge
Answer & solution

Answer: Root b; left f with left a; right d with left c and right e with left g. (no official key was published)

The final postorder symbol b is the root. Split inorder around b: left subtree has a,f and right subtree has c,d,g,e. The corresponding postorder chunks are a,f and c,g,e,d.

For the left subtree, final f is its root and a lies to its left, so f has left child a and no right child. For the right subtree, final d is its root. Inorder splits into c on its left and g,e on its right. The left child is c. The right chunk has postorder g,e, hence root e with left child g.

        b
       / \
      f   d
     /   / \
    a   c   e
           /
          g

Checking inorder yields a,f,b,c,d,g,e. Checking postorder yields a,f,c,g,e,d,b. The labels are distinct, so the recursive splits uniquely determine the tree. A different shape that merely shares the root would not suffice.

Original paper, page 7