Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The tree with edges 1-2, 1-3, 2-4, 2-5, 3-6, 6-7, 6-8 is rooted at node 1. How many nodes have exactly two neighbours (degree 2) in this tree?

  1. A. 3
  2. B. 1
  3. C. 2
  4. D. 4

Correct answer: C. 2

Degree counts the neighbours of a node in the undirected tree, regardless of the root. Counting children (out-edges from the root) gives a different number.

More Coding Interview Problems questions

Exams that ask this topic