L44. Delete a Node in Binary Search Tree | BST | C++ | Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ม.ค. 2025

ความคิดเห็น • 194

  • @takeUforward
    @takeUforward  3 ปีที่แล้ว +93

    Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79

    • @codeguy21
      @codeguy21 3 ปีที่แล้ว +1

      amazing brother

    • @namanlakhwani4752
      @namanlakhwani4752 3 ปีที่แล้ว +1

      At least let us know till when the rest of the videos will be uploaded? So that we can plan accordingly. I'm waiting from last 2 weeks

    • @h108pranavshinde3
      @h108pranavshinde3 10 หลายเดือนก่อน

      Its just Crystal clear and amazing !

  • @rohan8758
    @rohan8758 9 หลายเดือนก่อน +13

    Thanks striver for this Tree series & all of your other series, I wish that people like you should born again & again in every verse so if you can educate creature of that planet very well for at least one domain. Once again thanks to you & god bless you, live long & god will fullfill you with prosperity.

  • @NoBakwas
    @NoBakwas 2 ปีที่แล้ว +41

    Remember to take delete the node explicitly after detaching the links to prevent memory leaks.

  • @shqnz
    @shqnz 2 ปีที่แล้ว +31

    This is the best explanation I have ever seen on deleting a node from BST.... To those who are still confused... I recommend them watching it again with full concentration rather than jumping to other videos..
    Thanks @takeUforward😉

    • @KS0607
      @KS0607 ปีที่แล้ว +2

      but its very confusing...not the best method i can say..
      public class Solution {
      public TreeNode solve(TreeNode A, int B) {
      return deleteNode(A,B);
      }

      private TreeNode deleteNode(TreeNode A, int B){
      if(A == null){
      return null;
      }
      if(B < A.val){
      A.left = deleteNode(A.left,B);
      }
      else if(B > A.val){
      A.right= deleteNode(A.right,B);
      }
      else{
      if(A.left == null && A.right == null){
      return null;
      }
      else if(A.right == null){
      return A.left;
      }
      else if(A.left == null){
      return A.right;
      }
      else{
      TreeNode temp = findMaxfromLeft(A.left);
      A.val = temp.val;
      A.left = deleteNode(A.left, temp.val);
      }
      }
      return A;
      }
      private TreeNode findMaxfromLeft(TreeNode A){
      while(A.right != null){
      A = A.right;
      }
      return A;
      }
      }

  • @iamparitosh
    @iamparitosh 3 ปีที่แล้ว +61

    Completed all the 45 problems, thanks for the mashup striver.
    Didn't watch the video lectures but excellent problem choice for intermediate candidates

  • @raghav5095
    @raghav5095 12 วันที่ผ่านมา +3

    Striver code works well, here is an alternative code that also makes sure that the tree isn't left, right weighted.
    In my approach, we deal it in 2 Cases:
    1. Case 1: Node with Two Children
    If the node to be deleted has two children, we find the largest value in its left subtree (the rightmost node in the left subtree), replace the value of the node to be deleted with this largest value, and then recursively delete the duplicate node in the left subtree. This approach maintains the BST property and avoids skewing the tree.
    2. Case 2: Node with One or No Child
    If the node has only one child or no children, we directly replace the node with its child (or `nullptr` if no child exists). This handles both single-child and leaf node cases efficiently while preserving the BST structure.
    class Solution {
    public:
    TreeNode* deleteNode(TreeNode* root, int key) {
    TreeNode* ptr = root;
    if(root == NULL) return root;
    if(root->val < key){
    root->right = deleteNode(root->right,key);
    }
    else if (root->val > key){
    root-> left = deleteNode(root->left,key);
    }
    else{
    if(root->left == NULL){
    ptr = root;
    root = root->right;
    delete ptr;
    }
    else if(root->right == NULL){
    ptr = root;
    root = root->left;
    delete ptr;
    }
    else{
    ptr = root->left;
    while(ptr->right != NULL){
    ptr = ptr->right;
    }
    root->val = ptr->val;
    root -> left = deleteNode(root->left,ptr->val);
    }
    }
    return root;
    }
    };

  • @Kpriyasing
    @Kpriyasing 3 ปีที่แล้ว +18

    Completed upto here!
    And it's really worth watching ✨

  • @liftingMohit
    @liftingMohit 3 ปีที่แล้ว +8

    Bhaiya watched all 45 videos of Tree Series and I learnt a lot from them ... waiting for rest of the videos.. Thank You so much bhaiyaa

  • @amanshah1650
    @amanshah1650 5 หลายเดือนก่อน +1

    I really appreciate your videos and intuitions!!!
    Another approach :-
    TreeNode* deleteNode(TreeNode* root, int key) {
    if(!root)return NULL;
    if(!root->left&&!root->right&&root->val==key)return NULL;
    if(root->val==key)return helper(root);
    TreeNode*par=root,*curr=root;
    while(root){
    if(root->val==key){
    if(root->left){
    auto rMostOfLeft=root->left;
    while(rMostOfLeft->right){
    rMostOfLeft=rMostOfLeft->right;
    }
    rMostOfLeft->right=root->right;
    if(par->val>root->val)par->left=root->left;
    else par->right=root->left;
    }
    else{
    if(par->val>root->val)par->left=root->right;
    else par->right=root->right;
    }
    break;
    }
    if(key>root->val){par=root;root=root->right;}
    else {par=root;root=root->left;}
    }
    return curr;
    }
    TreeNode* helper(TreeNode*root){
    if(!root->left)return root->right;
    if(!root->right)return root->left;
    auto temp=root->left;
    while(temp->right)temp=temp->right;
    temp->right=root->right;
    return root->left;
    }

  • @jayatanwani5979
    @jayatanwani5979 3 ปีที่แล้ว +15

    Your content is worth watching striver, thank you for everything you do!

  • @vineelpynam6547
    @vineelpynam6547 2 ปีที่แล้ว +86

    Simple Code using same approach:
    TreeNode* deleteNode(TreeNode* root, int key) {
    if( !root ) return NULL;
    if( root->val < key ) root->right = deleteNode(root->right, key);
    else if( root->val > key ) root->left = deleteNode(root->left, key);
    else{
    if( !root->right && !root->left ) return NULL;
    else if( !root->right ) return root->left;
    else if( !root->left ) return root->right;
    else{
    TreeNode* temp = root->right;
    while( temp->left ) temp = temp->left;
    temp->left = root->left;
    return root->right;
    }
    }
    return root;

    }
    🙂

    • @AmanKumar-xb7cw
      @AmanKumar-xb7cw ปีที่แล้ว

      Replace this
      if( !root->right && !root->left ) return NULL;
      else if( !root->right ) return root->left;
      else if( !root->left ) return root->right;
      else{
      TreeNode* temp = root->right;
      while( temp->left ) temp = temp->left;
      temp->left = root->left;
      return root->right;
      }
      with
      TreeNode*temp=root->right;
      if(temp){
      while( temp->left ) temp = temp->left;
      temp->left=root->left;
      return root->right;
      }
      return root->left;

    • @Mel-up7un
      @Mel-up7un 3 หลายเดือนก่อน

      a not so simple solution >> hard to read soln

  • @_-6912
    @_-6912 3 ปีที่แล้ว +23

    Hi Raj, great content and epic series, could you kindly let us know if there are more videos coming for Binary search tree concepts?

    • @abhiprojectz2995
      @abhiprojectz2995 3 ปีที่แล้ว +2

      You have so much of money to waste, like wth joins a channel for a comment icon

    • @_-6912
      @_-6912 3 ปีที่แล้ว +2

      @@abhiprojectz2995 thanks I dint notice I was continuing membership. I just stopped it. It seems there are no more meet ups.

  • @saurabhmishra1587
    @saurabhmishra1587 2 ปีที่แล้ว +18

    For java people, I would suggest going for the recursive way...it's much easier to understand. Here is the code -
    public TreeNode deleteNode(TreeNode root, int key) {
    //if null i.e. empty tree, return null
    if(root == null){
    return null;
    }
    //keep moving left/right until you don't find the key
    if(key < root.val){
    root.left = deleteNode(root.left, key);
    }else if(key > root.val){
    root.right = deleteNode(root.right, key);
    }else{
    //if key is found, check if it's left/right is empty
    if(root.left == null){
    return root.right;
    }else if(root.right == null){
    return root.left;
    }
    //otherwise find min in right subtree, replace with cureent value and delete that min node in right subtree
    TreeNode minNode = findMin(root.right);
    root.val = minNode.val;
    root.right = deleteNode(root.right, root.val);
    }
    return root;
    }
    private TreeNode findMin(TreeNode node){
    while(node.left != null){
    node = node.left;
    }
    return node;
    }

  • @harrypottah9862
    @harrypottah9862 3 ปีที่แล้ว +8

    Striver is a cpp guy 9:34

  • @dingo_5998
    @dingo_5998 2 ปีที่แล้ว +2

    Thank you bhaiya 😊😊 Aapsey google may milu gaa😎😎😎

  • @ipawanbhatt
    @ipawanbhatt 2 ปีที่แล้ว +2

    What a crsip explanation.. Loved it

  • @lifegood6401
    @lifegood6401 3 ปีที่แล้ว +13

    Completed all these videos with notes as well, Please upload more you said you will upload 7-8 videos on weekend. Please do it if possible.

  • @aniketkhede6894
    @aniketkhede6894 2 ปีที่แล้ว

    meko kewal aapka hi samajh aata h aur koi ka kuch samajh ni padta thank you

  • @arjunsolanki752
    @arjunsolanki752 2 ปีที่แล้ว +2

    great explaination and code, thanks raj bhaiya❤️

  • @shivangisrivastava1158
    @shivangisrivastava1158 3 ปีที่แล้ว +5

    Wow as always, love your intuition and explanation

  • @paragroy5359
    @paragroy5359 3 ปีที่แล้ว +2

    Nice explanation your videos are really good...please keep on making such videos...you are doing a great job.

  • @shilpadam6415
    @shilpadam6415 ปีที่แล้ว

    Great Job, thanks all your efforts !!

  • @ArdentMusicLover
    @ArdentMusicLover 5 หลายเดือนก่อน

    Loved your intuition !

  • @gouravkushwaha68
    @gouravkushwaha68 ปีที่แล้ว

    Beautiful explanation

  • @anandtripathi
    @anandtripathi 3 ปีที่แล้ว +1

    waiting for next 8 videos, thanks man!

  • @108_adityakumar6
    @108_adityakumar6 2 ปีที่แล้ว +1

    Great content bhaiya 🙌

  • @arif2921
    @arif2921 11 หลายเดือนก่อน

    Amazing !!!! Explanation

  • @Ragehunter
    @Ragehunter ปีที่แล้ว

    bro this is so gold, thank you so much!

  • @temp1-f3u
    @temp1-f3u ปีที่แล้ว

    nice video thank you for such informative video

  • @ombhandari6148
    @ombhandari6148 2 ปีที่แล้ว

    Finally understood the concept,
    Thanks bhaiya ✨

  • @deveshdubey6776
    @deveshdubey6776 3 ปีที่แล้ว

    thanks bro..completed the series..thank you!!

  • @ritikshandilya7075
    @ritikshandilya7075 8 หลายเดือนก่อน

    Amazing bro , thanks

  • @VivekKumar-zo7kr
    @VivekKumar-zo7kr 3 ปีที่แล้ว +3

    All videos out?? Or is there any video yet to be uploaded?
    Btw great work..🔥🔥

  • @stith_pragya
    @stith_pragya ปีที่แล้ว +1

    Thank You So Much for this wonderful video........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @rishabhgoyal8110
    @rishabhgoyal8110 3 ปีที่แล้ว +1

    Thank you so much man! God Bless you!

  • @vaibhavpandey9779
    @vaibhavpandey9779 ปีที่แล้ว +3

    Instead doing this, we can find the minimum value in the right subtree or maximum value in the left subtree, copy its value in the node to be deleted and delete the minimum/maximum node. Here's the code:
    Node* Delete(Node* r, int value){
    if (r==NULL) return r;
    else if (valuedata) r->left = Delete(r->left,value);
    else if (value>r->data) r->right = Delete(r->right,value);
    else{
    if (r->left == NULL && r->right == NULL){
    delete r;
    r=NULL; //as it will be dangling
    } else if (r->left == NULL) {
    Node* temp = r;
    r = r->right;
    delete temp;
    } else if (r->right == NULL) {
    Node* temp = r;
    r = r->left;
    delete temp;
    } else {
    Node* temp = MinBstNode(r->right);
    r->data = temp->data;
    r->right = Delete(r->right,temp->data);
    }
    }
    return r;
    }

    • @blackstargaming3678
      @blackstargaming3678 ปีที่แล้ว

      basically u are saying to find kind of inorder successor am I right?

    • @vaibhavpandey9779
      @vaibhavpandey9779 ปีที่แล้ว

      @@blackstargaming3678 No, the inorder successor in BST would be the next largest node as inorder of BST is sorted. I am talking about the maximum node in the left subtree (because we have to maintain the BST property)

    • @rajjagtap7737
      @rajjagtap7737 ปีที่แล้ว

      @@vaibhavpandey9779 it can either be inorder predecessor or successor, doesn't really matter.

  • @ranjeetkumbhar
    @ranjeetkumbhar ปีที่แล้ว +1

    @striver, ❗❗❗❗BST does not allow duplicate values, so why are there two 8s in the example

  • @Kpriyasing
    @Kpriyasing 3 ปีที่แล้ว +7

    Amazing content!

  • @ryanaugustine5940
    @ryanaugustine5940 2 ปีที่แล้ว +27

    This is a bad stategy because the more you delete the more you make the tree unbalanced. In a balanced tree the time complexity for search is O(LogN) and in the most unbalanced tree (only left or right nodes) the time complexity is O(N).

    • @pulkitjain5159
      @pulkitjain5159 ปีที่แล้ว +6

      ya but intution wise it is great , another approach what we can do is just swap the greater numbers with the node until your node becomes leaf node , and at last delete this node.

    • @KaifKhan-sb2yr
      @KaifKhan-sb2yr 10 หลายเดือนก่อน

      How bro do you have a code ? ​@@pulkitjain5159

    • @kishoregowda8210
      @kishoregowda8210 3 หลายเดือนก่อน

      The question is asking us to just delete the node, so this approach is more than enough. If you want to maintain the log(N) time complexity, then you have to go with something like self balancing tree (AVL).

    • @WowSirSmallFan
      @WowSirSmallFan 2 หลายเดือนก่อน

      Don't remember asking

  • @mixshots1801
    @mixshots1801 10 หลายเดือนก่อน +2

    I did like this using C++ using the second approach
    ```
    /**
    * Definition for a binary tree node.
    * struct TreeNode {
    * int val;
    * TreeNode *left;
    * TreeNode *right;
    * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    * };
    */
    class Solution {
    public:
    TreeNode* deleteNode(TreeNode* root, int key) {
    if(root==NULL)return NULL;
    if(root->val==key){
    if(root->right){
    auto f = root->right;
    while(f->left!=NULL){
    f=f->left;
    }
    f->left=root->left;
    root->left=root->right;
    }
    return root->left;
    }
    else if(root->val>key) root->left=deleteNode(root->left,key);

    else root->right= deleteNode(root->right,key);
    return root;
    }
    };
    ```

  • @aryansinha1818
    @aryansinha1818 หลายเดือนก่อน

    Thank you

  • @abirpaul9027
    @abirpaul9027 2 ปีที่แล้ว +3

    Hey striver this is my viewed video of your channel.I have a doubt everywhere they are saying to bring the inorder predecessor or successor to delete a non leaf node and you are giving this approach which one should we go for?

    • @FusionArcs
      @FusionArcs 2 ปีที่แล้ว +1

      totally depends, whatever is easy for you to understand

  • @mohammedzaidkhan5687
    @mohammedzaidkhan5687 หลายเดือนก่อน

    nice video

  • @solankiketul5640
    @solankiketul5640 8 หลายเดือนก่อน

    I can think of another solution as well. We can replace the node to be deleted with the leaf node with the greatest value on the left subtree or the leaf node with the smallest value on the right subtree. Like in our example, replacing 5 with 4 and keeping the tree as it is. It will maintain the height difference of the tree

    • @AmanjotSingh-rj7ox
      @AmanjotSingh-rj7ox 5 หลายเดือนก่อน

      but what if the smallest or greatest values you are talking about are not leaf nodes and they have a child with them, they you might loose their childs. try with this example : [8,3,6,2,null,null,7]
      try deleting 8

    • @shayaanrk
      @shayaanrk หลายเดือนก่อน

      No bro, we have to consider all three cases: 0 child, 1 child and 2 children

    • @m_fi8926
      @m_fi8926 15 วันที่ผ่านมา

      @@shayaanrk yes thats kind of simpler than this

  • @JohnWick-kh7ow
    @JohnWick-kh7ow 3 ปีที่แล้ว +10

    I think the recursive solution would be short and better.
    Btw, I watched all videos of this playlist. Your videos are awesome as always. Please upload the remaining videos.

    • @pratyushnarain5220
      @pratyushnarain5220 3 ปีที่แล้ว +1

      No I checked the recursive one. It becomes too confusing

    • @suryakiran2970
      @suryakiran2970 2 ปีที่แล้ว

      Recursive solution
      Node* insertNode(Node* root, int newKey)
      {
      if(root == NULL)
      return new Node(newKey);

      if(newKey < root->data)
      root->left = insertNode(root->left, newKey);
      else
      root->right = insertNode(root->right, newKey);

      return root;
      }

    • @KS0607
      @KS0607 ปีที่แล้ว

      @@pratyushnarain5220 public class Solution {
      public TreeNode solve(TreeNode A, int B) {
      return deleteNode(A,B);
      }

      private TreeNode deleteNode(TreeNode A, int B){
      if(A == null){
      return null;
      }
      if(B < A.val){
      A.left = deleteNode(A.left,B);
      }
      else if(B > A.val){
      A.right= deleteNode(A.right,B);
      }
      else{
      if(A.left == null && A.right == null){
      return null;
      }
      else if(A.right == null){
      return A.left;
      }
      else if(A.left == null){
      return A.right;
      }
      else{
      TreeNode temp = findMaxfromLeft(A.left);
      A.val = temp.val;
      A.left = deleteNode(A.left, temp.val);
      }
      }
      return A;
      }
      private TreeNode findMaxfromLeft(TreeNode A){
      while(A.right != null){
      A = A.right;
      }
      return A;
      }
      }

    • @avanishrivastava4570
      @avanishrivastava4570 ปีที่แล้ว

      @@suryakiran2970 It isn't covering all the edge cases

  • @vishaljaiswar6441
    @vishaljaiswar6441 ปีที่แล้ว

    i love you striver🤟

  • @divyakarlapudi
    @divyakarlapudi 2 ปีที่แล้ว

    Great Video Thankyou so much.

  • @abhaythakur2597
    @abhaythakur2597 ปีที่แล้ว

    very well explained

  • @kekoHere0610
    @kekoHere0610 2 ปีที่แล้ว +1

    BST does not allow duplicate values, so why are there two 8s in the example

  • @LordRadhakrishnaa
    @LordRadhakrishnaa 2 ปีที่แล้ว

    Bhaiya you are genius 🎉

  • @codding32world50
    @codding32world50 2 ปีที่แล้ว

    It will go wrong if 14:21 3's right is suppose 9 how will you add 7 node to the right of it

  • @deepakantoj432
    @deepakantoj432 2 ปีที่แล้ว

    the question is to delete a node from BST
    my approach is that i'll first get the key node
    and then also get the parent node of the key node .
    connect the parent node's left or right to key node's right .
    and store the key node's left in a temp node
    i'll traverse to the very left of the newly connected parent's left and attach the temp .
    /**
    * Definition for a binary tree node.
    * public class TreeNode {
    * int val;
    * TreeNode left;
    * TreeNode right;
    * TreeNode() {}
    * TreeNode(int val) { this.val = val; }
    * TreeNode(int val, TreeNode left, TreeNode right) {
    * this.val = val;
    * this.left = left;
    * this.right = right;
    * }
    * }
    */
    class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
    TreeNode node = solve(root,key); // key node
    TreeNode n = helper(root,key); // parent node
    if(n.left != null)
    if( n.left.val == key)
    n.left = node.right;
    else if(n.right != null)
    if(n.right.val == key)
    n.right = node.right;
    TreeNode left = node.left;
    TreeNode cur = node.right;
    while(cur.left!=null)
    {
    cur = cur.left;
    }
    cur.left = left;
    return root;
    }
    public TreeNode helper(TreeNode root , int val)
    {
    if(root == null)
    return null;
    if(root.left!=null)
    {
    if(val == root.left.val)
    return root;
    }
    else if(root.right!=null){
    if(val == root.right.val)
    return root;
    }
    return val > root.val ? helper(root.right , val) : helper(root.left , val);
    }
    public TreeNode solve(TreeNode root , int val)
    {
    if(root == null)
    return null;
    if(val == root.val)
    return root;
    return val > root.val ? solve(root.right , val) : solve(root.left , val);
    }
    }
    where have i gone wrong anybody please help

  • @prasadm3614
    @prasadm3614 2 ปีที่แล้ว

    Pepcoding solution is pretty clean n easy

  • @kedarsatwik6938
    @kedarsatwik6938 ปีที่แล้ว

    Much an easy way to memorize the intution
    TNode* delMergeBST(TNode* root){
    // root is to be removed, so we will merge all leftSubTree to right subTree and return a node
    // returned node will act like subsitute for del Node

    if (root->left == NULL){
    return root->right;
    }
    if (root->right == NULL){
    return root->left;
    }

    // if both right, left not null
    // all left subtree nodes are lesser than all right subtree nodes

    TNode* leftSubTree = root->left;
    TNode* rightSubTree = root->right;
    TNode* temp = rightSubTree;
    // now move temp so that temp->left is null , so we can attach leftSubTree to it
    while (temp->left != NULL){
    temp = temp->left;
    }
    temp->left = leftSubTree;

    return rightSubTree;
    }
    TNode* del_prevNodeBST(TNode* root,int x){
    if (root == NULL) return NULL;
    TNode* temp = root;
    while (temp->left != NULL || temp->right != NULL){
    if (temp->left != NULL){
    if (temp->left->data == x){
    return temp;
    }
    }
    if (temp->right != NULL){
    if (temp->right->data == x){
    return temp;
    }
    }
    if (temp->data < x){
    temp = temp->right;
    }else if (temp->data > x){
    temp = temp->left;
    }
    if (temp == NULL){
    return NULL;
    }
    }
    return NULL;
    }
    TNode* deleteNodeBST(TNode* root, int x) {
    if (root == NULL) return NULL;

    if (root->data == x){
    return delMergeBST(root);
    }

    TNode* prevNode = del_prevNodeBST(root,x);
    if (prevNode == NULL) return root;

    if (prevNode->left != NULL && prevNode->left->data == x){
    prevNode->left = delMergeBST(prevNode->left);
    return root;
    }
    if (prevNode->right != NULL && prevNode->right->data == x){
    prevNode->right = delMergeBST(prevNode->right);
    return root;
    }

    return root;
    }

  • @BinaryNerd
    @BinaryNerd ปีที่แล้ว

    nice explanation

  • @Weirdvloggertrue
    @Weirdvloggertrue 3 ปีที่แล้ว

    Great Work!
    Next set of videos?

  • @Hrit
    @Hrit 2 ปีที่แล้ว

    Clean Explanation.

  • @TheBaljitSingh
    @TheBaljitSingh 13 วันที่ผ่านมา

    major parts implemented by myself.

  • @UECAshutoshKumar
    @UECAshutoshKumar ปีที่แล้ว +1

    Thank you sir

  • @harshadevarakonda7965
    @harshadevarakonda7965 2 ปีที่แล้ว +1

    It would be better understood if you had explained about inorder predecessor and inorder successor because eventually we are replacing the node to be deleted with either of the two .....

    • @KS0607
      @KS0607 ปีที่แล้ว

      public class Solution {
      public TreeNode solve(TreeNode A, int B) {
      return deleteNode(A,B);
      }

      private TreeNode deleteNode(TreeNode A, int B){
      if(A == null){
      return null;
      }
      if(B < A.val){
      A.left = deleteNode(A.left,B);
      }
      else if(B > A.val){
      A.right= deleteNode(A.right,B);
      }
      else{
      if(A.left == null && A.right == null){
      return null;
      }
      else if(A.right == null){
      return A.left;
      }
      else if(A.left == null){
      return A.right;
      }
      else{
      TreeNode temp = findMaxfromLeft(A.left);
      A.val = temp.val;
      A.left = deleteNode(A.left, temp.val);
      }
      }
      return A;
      }
      private TreeNode findMaxfromLeft(TreeNode A){
      while(A.right != null){
      A = A.right;
      }
      return A;
      }
      }

  • @manaskhare412
    @manaskhare412 2 ปีที่แล้ว +3

    UNDERSTOOD, AFTER 2 DAYS

    • @utkarsh_108
      @utkarsh_108 2 ปีที่แล้ว +1

      why did it take so long for you

  • @wonderfulquotes6484
    @wonderfulquotes6484 3 ปีที่แล้ว

    awesome explanation

  • @divyaagarwal3091
    @divyaagarwal3091 ปีที่แล้ว

    Understood!!

  • @PrinceKumar-el7ob
    @PrinceKumar-el7ob 3 ปีที่แล้ว +2

    thank u bhaiya :)

  • @harshitjaiswal9439
    @harshitjaiswal9439 11 หลายเดือนก่อน

    understood

  • @piyushacharya7696
    @piyushacharya7696 2 ปีที่แล้ว +3

    #HELP
    Could someone explain that if we want to remove the root node then how the code is working? like how the left part and right part is joining together. Eg: 10:19

    • @nikharbehera4038
      @nikharbehera4038 2 ปีที่แล้ว

      Same doubt

    • @anshumaan1024
      @anshumaan1024 ปีที่แล้ว +1

      as per striver's code : 8->right = 9->right, and node 9 will be removed
      so, tree will be [8,5,12,3,7,10,13,null,null,null,null,null,11], which is will be a BST

  • @adebisisheriff159
    @adebisisheriff159 11 หลายเดือนก่อน

    Thank striver!!!

  • @smeetkothari5160
    @smeetkothari5160 7 หลายเดือนก่อน +1

    Doesn't this fail for test case [5,3,6,2,4,null,7] and key = 5?

    • @smeetkothari5160
      @smeetkothari5160 7 หลายเดือนก่อน +1

      I had also add these to code: if(root.val == key) {
      return helper(root);
      }

  • @rohandevaki4349
    @rohandevaki4349 3 ปีที่แล้ว +3

    and you didnt explain for leaf node, single child etc , missed the important part

    • @jiteshjoshisde3154
      @jiteshjoshisde3154 2 ปีที่แล้ว +1

      Giving error while using tha same code to submit

  • @sarthakbhatia7888
    @sarthakbhatia7888 2 ปีที่แล้ว

    class Solution {
    public:
    TreeNode* deleteNode(TreeNode* root, int k) {
    if (!root) {
    return root;
    }
    if (root->val == k) {
    TreeNode* temp = root->right;
    while(temp && temp->left) {
    temp = temp->left;
    }
    if (temp) {
    temp->left = root->left;
    return root->right;
    } else {
    return root->left;
    }
    } else if (root->val > k) {
    root->left = deleteNode(root->left, k);
    } else {
    root->right = deleteNode(root->right, k);
    }
    return root;
    }

  • @ayushnath3768
    @ayushnath3768 2 ปีที่แล้ว

    This approach increases the height of the tree which is not commendable

  • @kishoregowda8210
    @kishoregowda8210 3 หลายเดือนก่อน

    Below is my solution.... easy to understand.
    public static Node deleteNode(Node node, int key) {
    if(node == null) return null;
    if(key == node.value) {
    Node temp = node.left;
    if(temp == null) return node.right;
    while (temp.right != null) temp = temp.right;
    temp.right = node.right;
    return node.left;
    }
    if(key < node.value) node.left = deleteNode(node.left, key);
    else node.right = deleteNode(node.right, key);
    return node;
    }

  • @prasadm3614
    @prasadm3614 2 ปีที่แล้ว +1

    How about swapping the node until it becomes a leaf and then delete ? This takes 0(h) only rt

    • @pulkitjain5159
      @pulkitjain5159 ปีที่แล้ว

      no bro , isi example mein swapping karke dekhlo , and just delete root node 8 , you will find things.

  • @Parth.Deshpande
    @Parth.Deshpande 3 ปีที่แล้ว +1

    /**
    * Definition for a binary tree node.
    * struct TreeNode {
    * int val;
    * TreeNode *left;
    * TreeNode *right;
    * TreeNode() : val(0), left(nullptr), right(nullptr) {}
    * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
    * };
    */
    class Solution {
    public:
    TreeNode* deleteNode(TreeNode* root, int key) {
    if(root==NULL){
    return NULL;
    }
    if(root->val == key){
    return helper(root);
    }
    TreeNode* dummy = root;
    while(root!=NULL){
    if(root->val>key){
    // it's on left side
    if(root->left!=NULL && root->left->val==key){
    root->left = helper(root->left);
    break;
    }else{
    root=root->left;
    }
    }else{
    //it's on right
    if(root->right!=NULL && root->right->val==key){
    root->right=helper(root->right);
    break;
    }else{
    root=root->right;
    }
    }
    }
    return dummy;
    }

    TreeNode* helper(TreeNode* root){
    if(root->left==NULL){
    return root->right;
    }
    if(root->right==NULL){
    return root->left;
    }
    TreeNode* rightside = root->right;
    TreeNode* lastright = findlastright(root->left);
    lastright->right = rightside;
    return root->left;
    }

    TreeNode* findlastright(TreeNode* root){
    if(root->right==NULL){
    return root;
    }
    return findlastright(root->right);
    }
    };

  • @vishaljain4642
    @vishaljain4642 3 ปีที่แล้ว +1

    Gazab ka tree series

  • @bindumahapatra8234
    @bindumahapatra8234 2 ปีที่แล้ว

    if a node asked to delete which is not present in tree, to address it inside while we can write if else-if else to break and return -1

  • @amanbhadani8840
    @amanbhadani8840 3 ปีที่แล้ว

    Very nice explanation as well as the code.

  • @optimus_prime01
    @optimus_prime01 7 หลายเดือนก่อน

    Good explaination

  • @SubhamYadav-q1r
    @SubhamYadav-q1r ปีที่แล้ว

    How is returning dummy gives the correct answer, it was as it is, there were no operations performed on that.

  • @RamKumar-kz8gg
    @RamKumar-kz8gg 3 ปีที่แล้ว

    bhaiya, baaki ke bhi videos upload krdo, complete krke agle topic pe badna hai..
    Please

  • @prathameshkodgire8642
    @prathameshkodgire8642 2 ปีที่แล้ว +9

    Java wale be like: Mein kya karu fir job chod du😂🤣

  • @Rieshu-l9i
    @Rieshu-l9i 10 หลายเดือนก่อน

    #striver rock

  • @harshitsamdhani2426
    @harshitsamdhani2426 2 ปีที่แล้ว

    understood great video

  • @ParichayPlate_Tak
    @ParichayPlate_Tak 3 ปีที่แล้ว

    striver how many video are still to come in this playlist

  • @nishantingle1438
    @nishantingle1438 3 ปีที่แล้ว +7

    It becomes frustrating to solve this if asked in an interview.

  • @devbhattacharya153
    @devbhattacharya153 2 ปีที่แล้ว

    Thanks a lot striver

  • @sans.creates
    @sans.creates 3 ปีที่แล้ว

    Thank You!

  • @amarjeetkumarsingh733
    @amarjeetkumarsingh733 ปีที่แล้ว

    Great Video

  • @shreyashachoudhary480
    @shreyashachoudhary480 3 ปีที่แล้ว

    Epic as always, strive!

  • @onlysparsh
    @onlysparsh ปีที่แล้ว

    How can a bst have same node values ? 8 & 8 again

  • @asifbasheerkhan2855
    @asifbasheerkhan2855 3 ปีที่แล้ว +3

    Bhaiya, when more videos will come ?

  • @AmanSharma-pb8hi
    @AmanSharma-pb8hi ปีที่แล้ว

    what if the given key is the root only?

  • @PratibhaSharma-h7b
    @PratibhaSharma-h7b หลายเดือนก่อน

    While writing inorder traversal post deletion i found 8 is repeated

  • @hitheshpk6030
    @hitheshpk6030 2 ปีที่แล้ว

    UNDERSTOOD

  • @SachinSingh-id8mf
    @SachinSingh-id8mf 2 ปีที่แล้ว

    thankyou bhaiya

  • @NikhilGupta-zo5jh
    @NikhilGupta-zo5jh 2 ปีที่แล้ว

    understood

  • @surajwaghmare4653
    @surajwaghmare4653 ปีที่แล้ว

    good

  • @iWontFakeIt
    @iWontFakeIt ปีที่แล้ว

    I solved this dividing into two parts
    Part 1: writing a function that can delete the root node of a bst and return new root node,
    Part 2: writing a function to search for a given key in bst and then use the function written in part 1 to make reconnections. I hope this helps a lot while solving this type of problems.
    class Solution {
    public:
    TreeNode* makeReconnections(TreeNode* root){
    if(root->left==NULL) return root->right;
    if(root->right==NULL) return root->left;
    TreeNode* rootRightChild = root->right;
    TreeNode* rootLeftChild = root->left;
    TreeNode* temp = rootLeftChild;
    while(temp->right) temp = temp->right;
    temp->right = rootRightChild;
    return rootLeftChild;
    }
    TreeNode* solve(TreeNode* root, int key){
    // base case
    if(!root) return NULL;
    if(root->val == key){
    // delete it
    return makeReconnections(root);
    }else if(root->val > key){
    root->left = solve(root->left, key);
    return root;
    }else{
    root->right = solve(root->right, key);
    return root;
    }
    }
    TreeNode* deleteNode(TreeNode* root, int key) {
    if(!root) return NULL;
    // search for the node and then delete
    return solve(root, key);
    }
    };
    Here deleteNode() is the function which calls the helpers solve() [part 2 function] which in turn uses the function makeReconnections() [part 1 function]

  • @unfixflip5332
    @unfixflip5332 3 ปีที่แล้ว

    Bhaiya diagonal traversal of binary tree bhi add krdo isme

  • @venutalla5932
    @venutalla5932 ปีที่แล้ว

    Tq sir