Binary Search Tree : Insertion

  • + 3 comments

    Luckily I completed in 20 lines

            Node current=root;
            Node last=root;
            while(current!=null){
                last=current;
                if(current.data<value)current=current.right;
                else current=current.left;
            }
        Node temp=new Node();
        temp.data=value;
        if(root==null)return temp;
        
        
        if(last.data<value)last.right=temp;
        else last.left=temp;
        
        return root;