Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

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 = 5Output:5Input :Value to be inserted = 1Output:5 / 1â€‹Input :Value to be inserted = 4Output: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

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

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

YesLoading...

AVL Tree Insertion

...