GATE / CS ARCHIVE

GATE CSE 1994 / QUESTION 8

Reconstruction from preorder and postorder

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

GATE 1994, Q8 Descriptive
Report issue

Tree Properties & Traversals

Question 8, GATE 1994: A rooted tree has nodes numbered 1 to 12 in preorder. Its postorder is 3,5,4,2,7,8,6,10,11,12,9,1. Reconstruct it, give every parent, and show the tree.
Tap to enlarge
Answer & solution

Answer: Parents: 1:none; 2:1; 3:2; 4:2; 5:4; 6:1; 7:6; 8:6; 9:1; 10:9; 11:9; 12:9. (no official key was published)

1. The final postorder entry 1 is the root. The next preorder value 2 starts its first child subtree; that subtree ends at 2 in postorder, so it contains {2,3,4,5}.

2. Within subtree 2, node 3 is first and is already a complete subtree. The next child 4 has postorder 5,4 and therefore child 5.

3. The next unused preorder node is 6. Its postorder block is 7,8,6, giving children 7 and 8.

4. The remaining child subtree of 1 is rooted at 9 and has leaves 10,11,12.

5. These are ordered children according to the given traversals. A general rooted tree is intended, not a binary-tree constraint.

Diagram / structural description

1
├─2
│ ├─3
│ └─4
│   └─5
├─6
│ ├─7
│ └─8
└─9
  ├─10
  ├─11
  └─12

Original paper, page 5