Insert a node in a BST | Practice | GeeksforGeeks

Given a BST and a key K. If K is not present in the BST, Insert a new Node with a value equal to K into the BST. 
Note: If K is already present in the BST, don't modify the BST.

Example 1:
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.

Example 2:
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.

Your Task:
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 Key K as input parameters and returns the root of the modified BST after inserting K. 
Note: The generated output contains the inorder traversal of the modified tree.

Expected Time Complexity: O(Height of the BST).
Expected Auxiliary Space: O(Height of the BST).

Constraints:
1 <= Number of nodes <= 105
1 <= K <= 106
0
kuldeepy104595 days ago
Node* insert(Node* root, int Key) {   // Your code here   if(root==NULL){       Node *temp= new Node(Key);       root=temp;       return root;          }   if(root->data==Key){       return root;   }   if(root->data<Key){       root->right= insert(root->right,Key);       return root;   }   if(root->data>Key){       root->left= insert(root->left,Key);       return root;   }   }
0
chhitizgoyal3 weeks ago
Java Solution.
   Node insert(Node root, int Key) {       if(root==null){           return new Node(Key);       }       if(root.data>Key){           root.left=insert(root.left, Key);       }       if(root.data<Key){           root.right=insert(root.right, Key);       }       return root;   }
0
yoginpcs203 weeks ago
 Node *head=root;   Node* x=root;   if(root==NULL)   {     head=new Node(k);     return head;   }   while(root!=NULL)   {       if(root->data==k)       return head;       else if(root->data>k)       {           x=root;           root=root->left;                  }       else       {           x=root;           root=root->right;                  }   }   if(k>x->data)   {       Node* ins=new Node(k);       x->right=ins;   }   else   {       Node* ins=new Node(k);       x->left=ins;   }   return head;
0
noviicee4 weeks ago
Can somebody help me in understanding
What is wrong with this solution??
 
#Function to insert a node in a BST. 
def insert(root, Key):
    # code here
    if root is None:
        # insert root Node
        root=Node(Key)
        return root
        
    if Key==root.data:
        return root
    
    elif Key<root.data:
        # left child
        if root.left:
            insert(Key, root.left)
        else:
            root.left=Node(Key)
            # since tree has no left child
            # we insert the left child
    else:
        # right child
        if root.right:
            insert(Key, root.right)
        else:
            root.right=Node(Key)
            # since tree has no right child
            # we insert the right child
        
    return root
0
prabhakarsati294261 month ago
def insert(root, Key):   # code here   if root==None:       return Node(Key)   elif root.data == Key:       return root   elif root.data>Key:       root.left = insert(root.left,Key)   else:       root.right = insert(root.right,Key)   return root
 
0
bhargabnath6911 month ago
Node* insert(Node* root, int Key) {
    // Your code here
    if(!root) return new Node(Key);
    if(root->data == Key) return root;
    if(Key > root->data){
        root->right = insert(root->right, Key);
        return root;
    }
    root->left = insert(root->left, Key);
    return root;
}
0
detroix072 months ago
Node* insert(Node* root, int key) {   // Your code here      Node* curr = root;   Node* prev = NULL;   Node* newnode = new Node(key);      while(curr!=NULL){    if(key==curr->data) return root;    else if (key > curr->data){     prev=curr;     curr=curr->right;    } else {     prev=curr;     curr=curr->left;    }   }      if(key > prev->data) {     prev->right = newnode;     return root;   }      if(key < prev->data){    prev->left=newnode;    return root;   }   }
0
hv656162 months ago
Node* insert(Node* root, int key) 
{
    if(root == NULL)
    {
        Node *n = new Node(key);
        return n;
    }
    else if(root->data <key)
    {
        root->right = insert(root->right,key);
    }
    else if(root->data > key)
    {
        root->left = insert(root->left,key);
    }
    return root;
}
0
joshiakash982 months ago
Node* insert(Node* root, int key) {
    Node* temp = new Node(key);
    Node* curr = root; 
    Node* parent=NULL;
    if(root==NULL) return temp;
    
    while(curr!=NULL){
        parent=curr;
        if(curr->data==key) return root;
        if(curr->data>key){
            curr=curr->left;
        }else if(curr->data<key){
            curr=curr->right;
        }
    }
    
    if(parent->data>key) parent->left=temp;
    else parent->right=temp;
    
    return root;
}

0
8siddharth92 months ago
Python easy solution -def insert(root, Key):   # code here   if root==None:       return Node(Key)   elif root.data ==Key:       return root   else:       if Key<root.data:           root.left = insert(root.left, Key)       elif Key>root.data:           root.right = insert(root.right, Key)   return root
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.