Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys

Problem: A tree with 5 nodes is given by the parent array p = [−1, 0, 0, 1, 1] (node 0 is the root). How many leaves does it have?

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

Correct answer: A. 3

Leaves are nodes that are no node's parent: 2, 3 and 4, so there are three leaves.

More Coding Interview Problems questions

Exams that ask this topic