GateAI
Trees

GATE CSE 2018 · Q20

NAT1 markMedium

Time

The postorder traversal of a binary tree is 8,9,6,7,4,5,2,3,1. The inorder traversal of the same tree is 8,6,9,4,7,2,5,1,3. The height of a tree is the length of the longest path from the root to any leaf. The height of the binary tree above is ________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 4

Trees

To find the height of the tree, we reconstruct the binary tree from the given traversals:

  • Postorder: 8,9,6,7,4,5,2,3,18, 9, 6, 7, 4, 5, 2, 3, 1

  • Inorder: 8,6,9,4,7,2,5,1,38, 6, 9, 4, 7, 2, 5, 1, 3

  1. The last element in postorder is the root: 1.

  2. In the inorder traversal, 1 splits the tree into:

    • Left subtree inorder: [8,6,9,4,7,2,5][8, 6, 9, 4, 7, 2, 5]

    • Right subtree inorder: [3][3]

  3. From the postorder traversal:

    • Left subtree postorder: [8,9,6,7,4,5,2][8, 9, 6, 7, 4, 5, 2] (root is 2)

    • Right subtree postorder: [3][3] (leaf node at depth 1)

  4. For root 2:

    • Inorder left: [8,6,9,4,7][8, 6, 9, 4, 7], right: [5][5]

    • Postorder left: [8,9,6,7,4][8, 9, 6, 7, 4] (root is 4), right: [5][5]

  5. For root 4:

    • Inorder left: [8,6,9][8, 6, 9], right: [7][7]

    • Postorder left: [8,9,6][8, 9, 6] (root is 6), right: [7][7]

  6. For root 6:

    • Inorder left: [8][8], right: [9][9]

    • Postorder left: [8][8], right: [9][9]

Now, let's trace the path from the root (node 1) to the deepest leaf (nodes 8 or 9):

Path: 1→2→4→6→8\text{Path: } 1 \to 2 \to 4 \to 6 \to 8
Path: 1→2→4→6→9\text{Path: } 1 \to 2 \to 4 \to 6 \to 9

The length of this path (number of edges from root to leaf) is 44.

Hence, the height of the binary tree is 4.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Trees previous year question is Q20 of the GATE CSE 2018 paper. Find more GATE CSE PYQs on Trees in Programming and Data Structures, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Trees PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Trees in GATE CSE

Trees has 13 GATE CSE PYQs across 11 papers from 2012 to 2026. 5 of them are 1-mark NAT questions like this one, and it is the only one in GATE CSE 2018.

How GATE tests Trees

More from GATE CSE 2018

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2018 · Q20 · NAT

The postorder traversal of a binary tree is 8,9,6,7,4,5,2,3,1. The inorder traversal of the same tree is 8,6,9,4,7,2,5,1,3. The height of a tree is the length of the longest path from the root to any leaf. The height of the binary tree above is …

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.