

Given an AVL tree, insert an element in the AVL Tree.
An AVL tree is a self-balancing binary search tree.
It has the following properties:
1. It has the property of the binary search tree , i.e for every node , the nodes in its left subtree is less than the node and nodes in the right subtree is greater than the current node.
2. The absolute difference between the height of left subtree and right subtree of any node is less than or equal to 1.
Read more about AVL Tree Here: https://en.wikipedia.org/wiki/AVL_tree
For example:

The numbers in white beside the nodes denote the height of the tree. We can see when we insert 7 in the tree it imbalances the tree at node 10 and hence we do a rotation to rebalance it. FInally we return the in order traversal of the final tree.
Note1.Do not print anything, just return the root node of the tree.
2.Your constructed tree will be checked by doing an in-order traversal of the tree from the returned root node.
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of input contains the elements of the tree in the level order form separated by a single space.
The second line contains an integer denoting the value to be inserted
If any node does not have left or right child, take -1 in its place. Refer to the example below.
Example:
Elements are in the level order form. The input consists of values of nodes separated by a single space in a single line. In case a node is null, we take -1 on its place.
For example, the input for the tree depicted in the below image would be :

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (-1).
Note :
1.The above format was just to provide clarity on how the input is formed for a given tree.
2.The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output Format
For each test case, the root node of the AVL tree with the given node inserted.
1 <= T <= 100
1<= N <= 3*10^3
Where ‘T’ is the total number of test cases and N denotes the number of nodes in the given binary tree.
Time limit: 1 second
2
50 20 60 10 -1 -1 -1 -1 -1
8
50 20 -1 -1 -1
60
8 10 20 50 60
20 50 60
Test Case 1:
Initially, the tree is:

We can see that the given Tree is an avl tree because for each node in the tree, the balance factor is less than 1.
For node 10 it is 0 (height of left subtree and right subtree is 0.)
For node 20 it is 1 (height of left subtree is 1 and right subtree is 0.)
For node 50 it is 1 (height of left subtree is 2 and right subtree is 1.)
For node 60 it is 0 (height of left subtree and right subtree is 0.)
After inserting the node the tree is:

After Inserting 8 the tree is imbalanced as:
For node 10 it is 1 (height of left subtree is 1 and right subtree is 0.)
For node 20 it is 2 (height of left subtree is 2 and right subtree is 0.)
For node 50 it is 2 (height of left subtree is 3 and right subtree is 1.)
For node 60 it is 0 (height of left subtree and right subtree is 0.)
The imbalance closest to the newly added node is at node 20. Now this occurs because of insertion in the left subtree of the left child so we need a right rotation to balance it.
The Balanced AVL tree is:

After balancing, the balance factors are as follows:
For node 10 it is 0 (height of left subtree 1 and right subtree is 1.)
For node 20 it is 0 (height of left subtree is 0 and right subtree is 0.)
For node 50 it is 1 (height of left subtree is 2 and right subtree is 1.)
For node 60 it is 0 (height of left subtree and right subtree is 0.)
We see that now all balance factors are less than or equal to 1 therefore the Tree is now balanced and hence an AVL tree.
We return the root node of the balanced AVL Tree and can verify that the in-order traversal of the AVL tree will be:{8 10 20 50 60 }
Test Case 2:
Initially, the tree is:

The Balance Factor are:
For node 20 it is 0 (height of left subtree is 0 and right subtree is 0.)
For node 50 it is 1 (height of left subtree is 1 and right subtree is 0.)
After inserting, the tree is:

The balance Factor are:
For node 20 it is 0 (height of left subtree is 0 and right subtree is 0.)
For node 50 it is 0 (height of left subtree is 1 and right subtree is 1.)
For node 60 it is 0(height of left subtree is 0 and right subtree is 0.)
Since this tree is already balanced, we return the root node of the tree and can verify that the in-order traversal will be {20,50,60}.
2
5 1 4 -1 -1 -1 -1
3
1 2 3 -1 -1 -1 4 -1 -1
5
1 3 5 4
2 1 3 4 5
Check Balance Factor and Insert
Keeping in mind the above idea, we can devise the following recursive approach:
O(log(n)), where ‘n’ is the number of nodes in the tree.
The rotation operations (left and right rotate) take constant time as only a few pointers are being changed there. Updating the height and getting the balance factor also takes constant time. So the time complexity of AVL insert remains same as BST insert which is O(h) where h is the height of the tree. Since the AVL tree is balanced, the height is O(Logn). So time complexity of AVL insert is O(Logn).
O(n), where ‘n’ is the number of nodes in the tree.
In the worst case, we can have ‘n’ elements in the recursive stack, therefore, the space complexity is of the order of ‘n’