GATE / CS ARCHIVE

GATE CSE 2021 SET 2 / QUESTION 16

GATE CSE 2021 Set 2 Q16

Data Structures · Tree Properties & Traversals · Full GATE CSE 2021 Set 2 paper

GATE 2021 Set 2, Q16 1 mark · NAT
Report issue

Tree Properties & Traversals

Question 16, GATE 2021: A complete binary tree has 7 nodes. A is the first three BFS-discovered nodes from root; B is the first three DFS-discovered nodes from root. Find |A−B|.
Tap to enlarge
Answer & solution

Official answer: 1

The complete seven-node tree has three levels. BFS first visits the root and both children. DFS discovery first visits the root, one child, and one grandchild. Therefore exactly one BFS member, the other child of the root, is absent from the first three DFS members, so |A−B|=1.

Name the root r, children l,u and grandchildren l1,l2,u1,u2. BFS gives {r,l,u}. A left-first DFS gives {r,l,l1}; right-first gives {r,u,u1}. In either case the intersection has two nodes and the BFS-only set has one. The result is independent of which child is explored first.

Original paper, page 64 · Answer key