GateAI
Trees

GATE CSE 2021 Set 2 · Q16

NAT1 markEasy

Time

Consider a complete binary tree with 7 nodes. Let AA denote the set of first 3 elements obtained by performing Breadth-First Search (BFS) starting from the root. Let BB denote the set of first 3 elements obtained by performing Depth-First Search (DFS) starting from the root. The value of ∣A−B∣|A - B| is ________.

Your answerNumerical answer

Enter a value to check

Solution

Step-by-step verified solution

Official answer: 1

Trees

A complete binary tree with 7 nodes has 3 levels:

  • Level 0: root node, say 11

  • Level 1: left child 22, right child 33

  • Level 2: children of 22 are 4,54, 5; children of 33 are 6,76, 7

  1. Breadth-First Search (BFS) visits nodes level by level:

    • Starting at the root, the first 3 nodes visited are the root and its two direct children (Level 0 and Level 1).

    • Thus, A={1,2,3}A = \{1, 2, 3\}.

  2. Depth-First Search (DFS) explores as deep as possible along each branch before backtracking:

    • Starting at the root (11), it visits a child (e.g., 22) and then a grandchild (e.g., 44).

    • Thus, the first 3 nodes visited are B={1,2,4}B = \{1, 2, 4\} (or {1,3,6}\{1, 3, 6\} if visiting right child first).

  3. Set Difference:

    A−B={1,2,3}−{1,2,4}={3}A - B = \{1, 2, 3\} - \{1, 2, 4\} = \{3\}
    ∣A−B∣=1|A - B| = 1

Therefore, the value of ∣A−B∣|A - B| is 1.

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 Q16 of the GATE CSE 2021 Set 2 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 2021 Set 2.

How GATE tests Trees

More from GATE CSE 2021 Set 2

Share this solution

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

GateAI solution cardGATE CSE 2021 Set 2 · Q16 · NAT

Consider a complete binary tree with 7 nodes. Let A denote the set of first 3 elements obtained by performing Breadth-First Search (BFS) starting from the root. Let B denote the set of first 3 elements obtained by performing Depth-First Search (DFS) starting from the root. The value of |A - B| 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.