Given a BST and a key. Insert a new Node with value equal to that key into the BST.
Input: 2 / \ 1 3 K = 4 Output: 1 2 3 4 Explanation: After inserting the node 4 Inorder traversal will be 1 2 3 4.
Input: 2 / \ 1 3 \ 6 K = 4 Output: 1 2 3 4 6 Explanation: After inserting the node 4 Inorder traversal of the above tree will be 1 2 3 4 6.
You don't need to read input or print anything. Your task is to complete the function insert() which takes the root of the BST and a Key as inputs and returns the root of the BST after inserting the Key value into it. The inorder traversal of the tree is printed as output which is taken care by the drivers code.
Expected Time Complexity: O(Height of the BST).
Expected Auxiliary Space: O(1).
1 <= Number of nodes <= 100000
1 <= K <= 1000000
Note: The Input/Output format and Example are given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.