Skip to content

Latest commit

 

History

History
39 lines (35 loc) · 898 Bytes

File metadata and controls

39 lines (35 loc) · 898 Bytes
TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root==NULL)
        {
            root=new TreeNode(val);
            return root;
        }

        TreeNode* cur=root;
        while(true)
        {
            if(cur->val <=val)
            {
                if(cur->right!=NULL) 
                    cur=cur->right;
                else
                {
                    cur->right=new TreeNode(val);
                    break;
                }
            }

            else
            {
                if(cur->left!=NULL)
                    cur=cur->left;

                else
                {
                    cur->left=new TreeNode(val);
                    break;
                }
            }
        }
        return root;
    }