Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: A binary tree is encoded in preorder with '#' for a missing child: "1,2,#,#,3,4,#,#,5,#,#". How many leaves (nodes with two missing children) does it have?

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

Correct answer: B. 3

Rebuild the tree from the preorder string, with each '#' consuming a missing child. A leaf is a value node whose two children are both absent; counting '#' tokens counts missing children, not leaves.

More Coding Interview Problems questions

Exams that ask this topic