Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Increasing Order Search Tree

Easy
0/40
Average time to solve is 15m
profile
Contributed by
6 upvotes
Asked in company
Amazon

Problem statement

Ninja has been given a binary search tree. Ninja has to rearrange the tree such that:

1. The leftmost node of the binary search tree becomes the root of the tree.
2. Every node has no left child.
3. Every node has only one right child.
For example:

As Ninja is busy, he asks you for help. Can you help Ninja to rearrange the binary search tree?

Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 10
1 <= N <= 10000
-100000 <= data <= 100000  

Where ‘T’ denotes the number of test cases, ‘N’ denotes the number of nodes in the binary tree and ‘data’ represents the value of each node of the binary search tree.

Time Limit: 1 second
Sample Input 1:
1
2 1 3 -1 -1 -1 -1
Sample Output 1:
1 -1 2 -1 3 -1 -1
Explanation for Sample Output 1:
For sample test case 1: 

Sample Input 2:
1 
10 7 12 4 9 -1 -1 -1 -1 -1 -1
Sample Output 2:
4 -1 7 -1 9 -1 10 -1 12 -1 -1
Explanation for Sample Output 2:
For sample test case 1: 

Full screen
Console