Geeksforgeeks

Timer is Running

Given a Binary Tree, convert it to Binary Search Tree in such a way that keeps the original structure of Binary Tree intact.

**Example 1:**

**Input:
**1
/ \
** **2 3**
Output: **1 2 3

**Example 2:**

Input:1 / \ 2 3 / 4Output:1 2 3 4Explanation:The converted BST will be 3 / \ 2 4 / 1

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **binaryTreeToBST()** which takes the root of the Binary tree as input and returns the root of the BST. The driver code will print** inorder** traversal of the converted BST.

**Expected Time Complexity:** O(NLogN).

**Expected Auxiliary Space:** O(N).

**Constraints:**

1 <= Number of nodes <= 1000

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

jainshubham52535 | 529 |

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

jots62 | 351 |

kirtidee18 | 1057 |

ykaushik | 752 |

aggarwalvatsal5 | 741 |

harbilassingh | 576 |

pmr_3689 | 570 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4827 |

Login to report an issue on this page.