Data Structures & AlgorithmsHardProduct-company placement

The POSTORDER traversal of a BST with distinct keys is 16, 27, 31, 56, 95, 79, 58, 51, 45, 32. What are the height of the tree (edges on the longest root-to-leaf path) and its number of leaves?

  1. A. Height 6, leaves 3
  2. B. Height 5, leaves 4
  3. C. Height 5, leaves 3
  4. D. Height 4, leaves 2

Correct answer: C. Height 5, leaves 3

The last key 32 is the root; split the rest into smaller and larger blocks and recurse (or insert the reversed postorder into a BST). The rebuilt tree has level order 32, 31, 45, 27, 51, 16, 58, 56, 79, 95: height 5, 3 leaves.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic