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
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)
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.)
most underrated channel🙂
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?
after swapping our logic of BST breaks, so if you call the deleteNode again, how will it delete? at 19:48
just wowww!!!
How you are handling a leaf node case in coding part ??
Amazing bro
great bhaiya
Y max no. Of ways wale question arrays or string me dp ke hote h kya??
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
what is the code for swapping? at 36?
Last maeh root ko q return krwaya hai
code submit nahi ho raha hai leetcode main
Are you from Gujarat
No....I am from West Bengal
can you share the code ?
copies style of love babbar
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;
}
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.)