How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space

前端 未结 11 1372
借酒劲吻你
借酒劲吻你 2021-01-31 22:12

How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space.

11条回答
  •  忘掉有多难
    2021-01-31 22:26

    Convert Binary Tree to a doubly linked list- can be done inplace in O(n)
    Then sort it using merge sort, nlogn
    Convert the list back to a tree - O(n)

    Simple nlogn solution.

提交回复
热议问题