AVL Tree Insertion
Hard Accuracy: 32.77% Submissions: 6734 Points: 8

Given a AVL tree and N values to be inserted in the tree. Write a function to insert a given value into the tree.

Example 1:

​N = 3
Values to be inserted = {5,1,4}

Input:      
Value to be inserted = 5
Output:
    5

Input :      
Value to be inserted = 1
Output:
    5
   /
  1

​Input :      
Value to be inserted = 4
Output:
    4
   / \
  1   5


Your Task:  
You dont need to read input or print anything. Complete the function insertToAVL() which takes the root of the tree and the value of the node to be inserted as input parameters and returns the root of the modified tree.

Note:
The tree will be checked after each insertion. 
If it violates the properties of balanced BST, an error message will be printed followed by the inorder traversal of the tree at that moment.
If instead all insertions are successful, inorder traversal of tree will be printed.


Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(height of tree)


Constraints:
1 ≤ N ≤ 500

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.