Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A tree with 6 nodes is given by parent array p = [−1, 0, 0, 2, 2, 4] for nodes 0–5 (root 0). What is the number of nodes whose subtree has size at least 2?

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

Correct answer: C. 3

Subtree sizes accumulate from the deepest nodes upward; a node has subtree size at least 2 exactly when it has at least one child. Counting leaves instead gives the complement count.

More Coding Interview Problems questions

Exams that ask this topic