Delete Nodes And Return Forest Solved | Simple Approach | Google | Leetcode 1110 | codestorywithMIK

preview_player
Показать описание
This is the 7th Video of our Playlist "BINARY TREE : Popular Interview Problems" by codestorywithMIK

In this video we will try to solve a very good Tree Problem : Delete Nodes And Return Forest Solved | Simple Approach | Google | Amazon | Leetcode 1110 | codestorywithMIK

I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.

Problem Name : Delete Nodes And Return Forest Solved | Simple Approach | Google | Amazon | Leetcode 1110 | codestorywithMIK
Company Tags : GOOGLE, AMAZON

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

Summary :
The given approach for deleting nodes from a binary tree and returning the forest of remaining trees involves a recursive helper function and a set for efficient node deletion lookup. Here’s a summary of the approach:

Convert to Set: The to_delete list of integers is converted to an unordered set st for efficient lookup of nodes to be deleted.

Recursive Deletion: The deleteHelper function is recursively called to traverse the tree. During traversal:

For each node, the function recursively processes its left and right children.
If a node's value is found in the set st, the node is deleted:
Its left and right children (if not null) are added to the result list as new roots.
The function returns NULL to effectively delete the current node.
If a node's value is not in the set, the function returns the node itself.
Check Root: After the initial call to deleteHelper, the root is checked:

If the root's value is not in the set st, it is added to the result list as a new root.
Result List: The function delNodes returns the list of remaining tree roots after deletions.

This approach ensures that nodes are deleted correctly while their children become new tree roots, forming a forest as the final result.

✨ Timelines✨
00:00 - Introduction
0:15 - Problem Explanation
2:05 - Explanaining Thought Process
9:25 - Coding it up

#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024
Рекомендации по теме
Комментарии
Автор

Bhai apki explanation best hoti hai. Much respect to you. mene question 20 mins. m kr liya tha but wrong answer arha tha baar baar. Pta hi nhi lgra tha mujhe kyu glt hai bhot chatgpt kiya yt par solutions dekhe kahi kuch smjh m nhi aya. 2 ghnte dimag lgane k baad bhi nhi hua. Fir apki video dekh kar click hua ki I just have to push root node of tree and what i was doing, I was pushing every node which is not present. All thanks to you just made some corrections in my code and it got submitted. ALL THANKS TO YOU.🙏🙏

Divyansh-xn
Автор

this question done :)
edit : this question came as POTD 17th July 2024

kondkartauqeer
Автор

best explanation for this question on YouTube

ajaybind
Автор

kudos to you..the way you make things easier to understand.

neelakshisoni
Автор

Sir jo last me aapne root ke kia check at the end of code uske jagah ye bhi kar sakte ha jo function call kia apne usko ek Treenode variable me store kara ke then last me check kar lete if that variable is not null add into our answer vector 😊

jeehub
Автор

Class solution {
Public list<Treenode >delNode(Treenode root, int[]to-delte){
set<interger >set=new Hash set<>();
for(int i:to-delete){ set.add(i);
list <Treenode >res=new Array list <>();
if(!set.contain(root.value)) res.add(root)
dfs(root, set, res)
return res;
}

Private Treenode dfs(TreeNode node, set<interger >set, list <TreeNode>res)
{ if(node==null){return null
}:
node.left=dfs(node.left, set, res);
node.right=dfs(node.right, set, res);
if(set.contain(node.val)){


return null
}
return node
}
};

dayashankarlakhotia
Автор

can you please also solve Selling Pieces of Wood ? thanks!

floatingpoint
Автор

sorry bhaiya but I dont watch your code part,





Aap approach hi itna acha smjha dete ho, ki code dekhne ki need hi nhi lgti 😀

sehajdeepsingh
Автор

sir deletehelper me hum tree ka function kye baaye hain jabkie return void ke tareah kar rahe hai

Squid_suraj
Автор

is tarah ke questions me logic nhi bana paate hai. How do I improve it?

AnshuHarshit
Автор

Solved it using parent node. Here is the python code:
class Solution:
def delNodes(self, root: Optional[TreeNode], to_delete: List[int]) -> List[TreeNode]:
res = []

def dfs(node, parent, isLeft):
if not node:
return

dfs(node.left, node, 1)
dfs(node.right, node, 0)

if node.val in to_delete:
if parent and isLeft:
parent.left = None
elif parent and not isLeft:
parent.right = None

nonlocal res

if node.left:
res.append(node.left)

if node.right:
res.append(node.right)

dfs(root, None, 0)

if root.val not in to_delete:
res.append(root)

return res

sauravchandra
Автор

class Solution {
public:
vector<TreeNode*> ans;
unordered_set<int> toDeleteSet;

TreeNode* deleteNodes(TreeNode* root) {
if (root == NULL) {
return NULL;
}
// pahale niche jaate jao ;
// (if cnd) agar upar likh di toh parent delete
// krne ke baad child ko access nhi kar paaenge
root->left = deleteNodes(root->left);
root->right = deleteNodes(root->right);

if (toDeleteSet.find(root->val) != toDeleteSet.end()) {
if (root->left)
ans.push_back(root->left);
if (root->right)
ans.push_back(root->right);
delete root;
return NULL;
}

return root;
}

vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
if (root == nullptr)
return {};

for (int val : to_delete) {
toDeleteSet.insert(val);
}

root = deleteNodes(root);
if (root) {
ans.push_back(root);
}

return ans;
}
};

chitranshjain
welcome to shbcf.ru