GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Reconstruct tree using inorder splits

3 questions solved the same way, newest first. Back to Data Structures

How these are solved

Take the root from preorder start or postorder end, split inorder at that root, recursively reconstruct subtrees, then emit the requested tree/postorder/height. The ordered nonbinary 1994 problem is deliberately excluded.

1 mark · NAT
Report issue

Reconstructing a binary tree height

Tree Properties & Traversals

Question 20, GATE 2018: Postorder is 8,9,6,7,4,5,2,3,1 and inorder is 8,6,9,4,7,2,5,1,3. Height counts edges on a longest root-to-leaf path. Find the height.
Tap to enlarge
Answer & solution

Official answer: 4

Method

Postorder ends with the root, and inorder splits each subtree into left part, root, right part. Root 1 splits off right leaf 3. Its left subtree ends in 2, whose inorder split leaves right leaf 5 and left subtree rooted at 4. Continuing gives 4 with children 6 and 7, and 6 with children 8 and 9. There are five vertices but four edges on the longest path, so do not report the number of levels.

Solution

The final postorder element gives root1, with right child3 and left subtree rooted 2. In that left subtree, 5 is the right child, while 4 is the left child of 2; 4 has left child6 and right child7, and 6 has children 8, 9. A longest path is 1→2→4→6→8, four edges. Thus height 4.

Original paper, page 9 · Answer key

2 marks · MCQ
Report issue

Postorder from inorder and preorder

Tree Properties & Traversals

Question 39, GATE 2007: A binary tree has inorder traversal d,b,e,a,f,c,g and preorder traversal a,b,d,e,c,f,g. What is its postorder traversal?
Options:
(A) d,e,b,f,g,c,a
(B) e,d,b,g,f,c,a
(C) e,d,b,f,g,c,a
(D) d,e,f,g,b,c,a
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Approach. The first preorder item is the root. Its position in inorder separates left and right subtrees; recursively repeat, then output left subtree, right subtree, root.

2. Derivation and interpretation. Preorder starts with root a. Inorder splits the left subtree as d,b,e and the right as f,c,g. The remaining preorder makes b the left root with children d,e, and c the right root with children f,g. Postorder is d,e,b,f,g,c,a, option A.

Original paper, page 9

5 marks · Descriptive
Report issue

Reconstruct traversal tree

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