Data Structures & AlgorithmsVery hardProduct company OA

Each array is a binary tree in level order (0-indexed, children at 2i + 1 and 2i + 2, all positions present). Which one is a valid BST?

  1. A. [20, 10, 30, 5, 25, 22, 35]
  2. B. [20, 10, 30, 5, 15, 18, 35]
  3. C. [20, 10, 30, 12, 15, 22, 35]
  4. D. [20, 10, 30, 5, 15, 22, 35]

Correct answer: D. [20, 10, 30, 5, 15, 22, 35]

Every key must respect ALL its ancestors, not just its parent. [.., 25, ..]: 25 > 10 is fine locally but sits in 20's LEFT subtree. [.., 18, ..]: 18 < 30 is fine locally but sits in 20's RIGHT subtree. [.., 12, ..]: 12 is 10's LEFT child but larger than 10. Only [20, 10, 30, 5, 15, 22, 35] passes the (low, high) bound check at every node.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic