Given a binary tree whose inorder and preorder traversal are given by Inorder…

2011

Given a binary tree whose inorder and preorder traversal are given by

Inorder : EICFBGDJHK

Preorder : BCEIFDGHJK

The post order traversal of the above binary tree is

Answer: A. IEFCGJKHDBConcept. In a preorder traversal the first symbol of any block is the root of that subtree. In an inorder traversal that same root splits its block into two…

  1. A.

    IEFCGJKHDB

  2. B.

    IEFCJGKHDB

  3. C.

    IEFCGKJHDB

  4. D.

    IEFCGJKDBH

Attempted by 28 students.

Show answer & explanation

Correct answer: A

Concept. In a preorder traversal the first symbol of any block is the root of that subtree. In an inorder traversal that same root splits its block into two parts: every symbol before it belongs to the left subtree and every symbol after it belongs to the right subtree. Applying these two facts together, recursively, reconstructs the tree uniquely. A postorder traversal then reports each subtree as left subtree, right subtree, root.

Application to this item.

  1. Preorder begins with B, so B is the root. In the inorder string EICFBGDJHK, B splits it as EICF | B | GDJHK, so the left subtree holds {E, I, C, F} and the right subtree holds {G, D, J, H, K}.

  2. The left subtree has 4 nodes, so it takes the next 4 preorder symbols: C, E, I, F. Its root is C. In its inorder part EI | C | F, C has left {E, I} and right {F}, so F is a leaf.

  3. For {E, I}: preorder E, I makes E the root, and inorder E | I puts I after E, so E has no left child and I as its right child.

  4. The remaining preorder D, G, H, J, K is the right subtree. Its root is D. In its inorder part G | D | JHK, D has left {G} and right {J, H, K}, so G is a leaf.

  5. For {J, H, K}: preorder H, J, K makes H the root, and inorder J | H | K gives H the left child J and the right child K.

The reconstructed tree.

Node

Left child

Right child

B (root)

C

D

C

E

F

E

none

I

D

G

H

H

J

K

Cross-check. Reading this tree in inorder gives E I C F B G D J H K and in preorder gives B C E I F D G H J K, which reproduce both sequences supplied in the question, so the reconstruction is the right one.

Postorder. Visiting left subtree, then right subtree, then root: the C-subtree yields I, E (E after its right child I), then F, then C, i.e. IEFC. The D-subtree yields G, then J, K, H, then D, i.e. GJKHD. Finally the root B. The postorder traversal is therefore IEFCGJKHDB.

Explore the full course: Coding For Placement

Loading lesson…