Data Structures & AlgorithmsVery hardProduct company OA

Binary trees with node labels A, B, C have preorder A B C and postorder C B A. How many different binary trees match BOTH traversals?

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

Correct answer: A. 4

Preorder + postorder cannot tell a lone LEFT child from a lone RIGHT child. Here B must be A's only child and C must be B's only child (any branching would put C before B in postorder). Each of the 2 single-child links can be left or right: 2 × 2 = 4 trees. Preorder + inorder would fix the tree uniquely.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic