Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A rooted tree (root 1) has children lists: 1: [2, 3, 4], 2: [5, 6], 3: [], 4: [7], 5: [], 6: [8, 9], 7: [], 8: [], 9: []. How many nodes have an even subtree size (the node itself counts toward its subtree)?

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

Correct answer: A. 1

Compute each subtree size recursively, then count the even values. Counting only the leaves or using depth instead of subtree size gives a different answer.

More Coding Interview Problems questions

Exams that ask this topic