Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys

Problem: A rooted tree of 8 nodes (root 1) is given by children lists: 1: [2, 3], 2: [4, 5], 3: [6], 4: [], 5: [7, 8], 6: [], 7: [], 8: []. What is the size of the subtree rooted at node 5 (node 5 counts itself)?

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

Correct answer: D. 3

Subtree size is 1 plus the sizes of the children's subtrees. Counting only the leaves under node 5 leaves out the node itself and the internal nodes.

More Coding Interview Problems questions

Exams that ask this topic