Data Structures & AlgorithmsModerateAmazon · Microsoft OA

A binary tree with 12 nodes is serialised in preorder, writing '#' for every missing child. How many '#' markers appear?

  1. A. 12
  2. B. 13
  3. C. 11
  4. D. 24

Correct answer: B. 13

Each of the n nodes has 2 child slots, 2n in total, and exactly n − 1 of them are filled by real nodes (every node except the root has a parent). So 2n − (n − 1) = n + 1 = 13 slots are empty. This is also why a valid serialisation has one more '#' than nodes.

More Coding Interview Problems questions

Exams that ask this topic