Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: A rooted tree has root 1 and children 1: [2, 3, 4], 3: [5], 4: [6, 7]. The number of nodes on the longest root-to-leaf path (including the root and the leaf) is what value?

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

Correct answer: D. 3

Height counted in nodes: the path 1-4-6 has three nodes, which is the longest. Counting edges along that path gives 2, one less than the node count asked for.

More Coding Interview Problems questions

Exams that ask this topic