Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: The right-side view of a binary tree lists the rightmost node at each depth from top to bottom. For the tree with root 1, children 2 and 3, node 2 has right child 5, node 3 has right child 4, and node 5 has left child 6, what is the sum of the values in the right-side view?

  1. A. 8
  2. B. 21
  3. C. 15
  4. D. 14

Correct answer: D. 14

A level-order BFS takes the last node of each level: 1, 3, 4 (not 5, which sits left of 4) and 6, summing to 14. Following only right-child pointers stops at 1, 3, 4 (8), summing all nodes gives 21, and taking 5 at depth 2 gives 15.

More Coding Interview Problems questions

Exams that ask this topic