The GATE Grind

GATE 2021 CS (CS2) – Question 26

Programming and Data Structures · Trees and Binary Search Trees · 1 mark · Numerical answer

In a complete binary tree with seven nodes, A is the set of the first three nodes visited by root-started BFS and B the set of the first three visited by root-started DFS. Find |A−B|.

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 1 to 1

Explanation

BFS first visits the root and its two children. DFS first visits the root, one child and one grandchild on that child’s side. Thus the root and one child are shared, leaving the other root child in A but not B. Therefore |A−B|=1, independent of whether left or right is explored first.

More Trees and Binary Search Trees questions

All Trees and Binary Search Trees questions →