Deletion of a Node in a BST | Binary Search Tree | Very Important | Babbar DSA Sheet | Amazon 🔥

Поделиться
HTML-код
  • Опубликовано: 8 ноя 2024

Комментарии • 19

  • @vishalkumar-sz2ko
    @vishalkumar-sz2ko 2 года назад +2

    most underrated channel🙂

  • @rohandevaki4349
    @rohandevaki4349 2 года назад +2

    how did you write delete(root) again ? if you are already writing the code for delete at 17:12 ,where did that function come from?

  • @rohandevaki4349
    @rohandevaki4349 2 года назад +2

    after swapping our logic of BST breaks, so if you call the deleteNode again, how will it delete? at 19:48

  • @harshitavyas7497
    @harshitavyas7497 2 года назад +1

    just wowww!!!

  • @ayushjain386
    @ayushjain386 Год назад

    How you are handling a leaf node case in coding part ??

  • @Nikita-ny9fo
    @Nikita-ny9fo Год назад

    Amazing bro

  • @Ayush-ro7bw
    @Ayush-ro7bw 3 года назад +2

    great bhaiya

  • @himanshugupta7647
    @himanshugupta7647 3 года назад

    Y max no. Of ways wale question arrays or string me dp ke hote h kya??

  • @prabhatsingh2606
    @prabhatsingh2606 3 года назад +1

    Ek aur option hai 5 ko delete karne ke liye hum inorder predecessor ki value 5 ke place par copy kar de fir left sub tree me search kar ke delete kar de

  • @rohandevaki4349
    @rohandevaki4349 2 года назад

    what is the code for swapping? at 36?

  • @MohitKumar-cf5hp
    @MohitKumar-cf5hp 2 года назад

    Last maeh root ko q return krwaya hai

  • @dipanjanbera5493
    @dipanjanbera5493 3 года назад

    code submit nahi ho raha hai leetcode main

  • @rahulmakwana9813
    @rahulmakwana9813 3 года назад

    Are you from Gujarat

  • @rohandevaki4349
    @rohandevaki4349 2 года назад

    can you share the code ?

  • @aryanverma7800
    @aryanverma7800 3 года назад

    copies style of love babbar

  • @nitinkumar5974
    @nitinkumar5974 3 года назад

    OPTIMIZATION :
    Bhaiya last mai Inorder Successor ko baar baar delete karne se acha ek baar mai hi delete kar do . Ek baar mai swap karke , phir uske baad delete kardo,, uska parent store karna padega bas.
    My Leetcode Submission :
    TreeNode* inSucc(TreeNode* p , TreeNode* &succParent)
    {
    p = p->right;
    while(p->left!=nullptr)
    {
    succParent = p;
    p = p->left;
    }
    return p;
    }

    TreeNode* deleteNode(TreeNode* root, int key)
    {
    if (root == NULL)
    return root;

    if (key < root->val)
    root->left = deleteNode(root->left, key);
    else if (key > root->val)
    root->right = deleteNode(root->right, key);
    else
    {
    // node with only one child or no child
    if (root->left == NULL)
    {
    TreeNode* temp = root->right;
    delete(root);
    return temp;
    }
    else if (root->right == NULL)
    {
    TreeNode* temp = root->left;
    delete(root);
    return temp;
    }
    else
    {
    // node with Two children: Get the inorder successor (smallest in the right subtree)

    TreeNode* succParent = root;
    TreeNode* succ = inSucc(root , succParent);

    swap(root->val ,succ->val);

    // Optimized code
    if (succParent != root)
    succParent->left = succ->right;
    else
    succParent->right = succ->right;

    delete (succ);
    }
    }

    return root;
    }

    • @namansharma5128
      @namansharma5128 2 года назад

      Have u considered the case when after swapping the successor would have two children. ( connect a node to the left of 6 in the original tree and dry run it.)