Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: A binary tree is given by a parent array where node i has parent p[i] (p[0] = −1 is the root): p = [−1, 0, 0, 1, 1, 2, 2, 5]. How many leaves does the tree have (a leaf has no children)?

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

Correct answer: A. 4

A leaf is a node that never appears as anyone's parent. Counting nodes whose parent index is larger, or nodes with the parent index 0, misidentifies leaves.

More Coding Interview Problems questions

Exams that ask this topic