题目描述
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:
5 4
/ LR rotation / \
1 -----------> 1 5
\
4
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
输入格式
见题面
输出格式
见题面