Monday, October 1, 2012

Binary Tree to BST Conversion


Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of Binary Tree.

Example 1--------------
Input:
          10
         /  \
        2    7
       / \
      8   4
Output:
          8
         /  \
        4    10
       / \
      2   7


Example 2---------------
Input:
          10
         /  \
        30   15
       /      \
      20       5
Output:
          15
         /  \
       10    20
       /      \
      5        30

http://www.geeksforgeeks.org/binary-tree-to-binary-search-tree-conversion/

No comments:

Post a Comment