1

I think it is not possible because if it actually is, people would build max-heap and then use it to construct BST which I think is not the case.

Please give an answer with a proof.

Vimal Patel
  • 211
  • 2
  • 9

1 Answers1

4

No you cannot.

The bottom level of the heap, which can contain over half the nodes, may be completely unordered in the heap. (imagine that all the internal nodes are less than all the leaf nodes).

Building a BST would determine the order of these nodes, but sorting takes O(n log n) time, so you cannot build a BST in O(n) time.

Matt Timmermans
  • 53,709
  • 3
  • 46
  • 87
  • Your answer seems correct but I think that nodes except leaf nodes should be all greater than leaf nodes(in your answer you seem to be mentioning about min-heap) cause question is about max-heap not about min-heap. However that's not a big mistake I think. Please correct me if I'm wrong. – Vimal Patel Feb 06 '19 at 03:08
  • 1
    Yes, sorry I was thinking a min-heap. With max-heap the last level can have any order if all the internal nodes have greater values than all the leaves, and the answer is the same. – Matt Timmermans Feb 06 '19 at 03:14