Lecture 67: Minimum Time to BURN the Entire Binary Tree || C++ Placement Series
ฝัง
- เผยแพร่เมื่อ 23 พ.ย. 2024
- In this Video, we are going to solve TREE Important Interview Questions.
There is a lot to learn, Keep in mind “ Mnn bhot karega k chor yrr apne se nahi hoga ya maza nahi aara, Just ask 1 question “ Why I started ? “
Visit Crio: www.crio.do/re...
Discord Server Link: / discord
Course Flow: whimsical.com/...
Notes Link: drive.google.c...
Slides Link: drive.google.c...
Code Links: github.com/lov...
Questions Links:
Min time to BURN tree: practice.geeks...
Do provide you feedback in the comments, we are going to make it best collectively.
Connect with me here:
Instagram: / lovebabbar1
Twitter: / lovebabbar3
Telegram Group Link: Love Babbar CODE HELP
telegram.me/lo...
My Editor: www.instagram....
Intro Sequence: We have bought all the required Licenses of the Audio, Video & Animation used.
#DSABusted #LoveBabbar
First I have completed all DSA series and now conclude this is Greatest ever DSA series to exist on youtube or paid courses. Your contribution will be remembered. You're God of DSA for us🙇♂ Thanks you.
If anyone wondering without adding value to map for all the nodes how can we check whether a node is visited or not here is the explanation :
If you try to access a key value using indexing operator [], then 2 things can happen :
1->The map contains this key. So it will return the corresponding key value
2->The map doesn't contain the key. In this case it will automatically add a key to the map with key value null.
i was coding this question by myself and just not stored q.size() before entering into for loop ....int the for loop new elements are getting pushed and whole function was working differently.....but thanks for teaching how to debug code by printing imp things...it took me 40 mins ...but the job was done......keep working hard...cheers..
Finally I am finishing my series of Binary trees
thanks a lot bhaiyaa ji
i have understood this concept finally
now i am moving towards BST
Your explanation was this good that I coded the solution myself by just seeing the algorithm🔥
Really??
@@pkyadav6230either he is better than us or he could be lying.
Don't stress yourself if you weren't able to come up with a solution yourself.
You should be Appreciated for explaining the toughest sum with such a ease
Absolutely love the way you make these hard problems seem so simple!! Thank you so much for such efforts!!🔥
Agree
I was able to write the code myself after watching the first half of the video. Thanks for the awesome content!
A Single thank you comment is not enough for the efforts bhaiya don't worry about views as people are left behind becoz of your consistent nature and Big Thumbs Up 🔥🔥
Sabd kamm parr jaa rhe hai aapki dedication dekh kr ❤️
This question was asked by me 2 years back in Paytm SDE 1 interview.
To be honest bhtt aasan laga jaise apne sikhaya bhaiyaa💖💖
Loving the series bhaiya! Aapki consistency hume inspire karti hai aur mehnat marne ke liye
sir aaj meko 1 and half month ho gya consist hu course kafi accha bana hai i like it . samaz nahi aa rha to me 3-4 bar piche ja kr dekh rha hu but koi boriat nahi ho rahi , love babber ke course ko love dher sara
i did with pair function
TC: O(n) //when unordered_map is used insted of map
SC: O(n)
thoda piche reh gya hun pr dheere dheere cover kr rha hun end takk complete krunga course ko keep uploading babbar bhai
Your explanation is the best!! Thanks for this amazing playlist!!
You're continuing despite less views in later vedios , 👏👏👏
babbar bhayya is love 💝
I think a better solution is solving using recursion after creating mapping with parent and finding target node as done in the video. What we can do is pass cur node and prev node in recursion and recursively check for left right and parent unless any of them is equal to prev node and then take the max time from the generated branches.The code will look like
class Solution {
int minBurnTime(Node* cur,Node* prev,map &m)
{
if(cur==NULL)
return 0;
int time=0;
if(m[cur]!=prev)
time=minBurnTime(m[cur],cur,m);
if(cur->left!=prev)
time=max(time,minBurnTime(cur->left,cur,m));
if(cur->right!=prev)
time=max(time,minBurnTime(cur->right,cur,m));
return time+1;
}
public:
int minTime(Node* root, int target)
{
if(root==NULL)
return 0;
map m;
queue q;
Node* tar;
q.push(root);
m[root]=NULL;
while(!q.empty())
{
Node* t=q.front();
q.pop();
if(t->data==target)
tar=t;
if(t->left)
{
m[t->left]=t;
q.push(t->left);
}
if(t->right)
{
m[t->right]=t;
q.push(t->right);
}
}
return minBurnTime(tar,NULL,m)-1;
}
};
Dhamaal machaa rha hai Course bhaiya!!!
Hard level question bhi easy laga... aur pehli baar me he samajh aagya.. thanks to your explanation and to your dedication to give us such a good content!!!
bro easy is liye lga because he taught the brute force soln.
optimized m dfs lagana h without using any extra space.
Aise hi h gfg pe HARD ka tag nhi lga hua.
@@pranayavnish8028tbhi itna lamba solution bana hai
Par kar bhi kya skte hai jb tk graphs pe na poch jaye hum
@@Ash_9202 vai graphs kaha se aagya bich m?? Ye video wala soln to 1st year ke students v khud se bna lenge,
This soln(as the optimal) will surely get u rejected in an interview , check leetcode discussions.
@@pranayavnish8028 agr koi 1st year student kr skta hai to pkka intelligent hoga bhai.
Mujhe to aise solution ki Intuition hi nahi aaegi. I guess I'm not that intelligent.
space complexity is becoming n but allowed space complexity is O(height of tree)
Present bhaiya
Consistency op 🔥🔥🔥🔥
Commenting for a better reach
I can say today😜 Maine toh aag lga di binary tree ko 😂😂😂😂
🔥🔥🔥🔥
Thanks you sir, excellent content, no one can explain like you from basic 🙏 god bless you
Aaj to aag lagadi bhai. dhuwa dhuwa kardiya binary tree ka
Maja aa raha hai
Aapki consistency ++
in this code no significance of bool flag because by help of flag we only save last iteration
and last iteration will be save by only q.size() if no element will push in queue then this is the case when queue is empty
Yeah, simply we can increment time every time when !q.empy()
best explanation for burning tree on utube
Instead of using vis as a map, you can use a set and check ki agar set mein voh node hai ki nahi, it will save space
I am thinking mai ise adjancy list bana kr bsf chala dun usse bhi ho jayega sayad.
Superbbbbb !!! Thank you so much for all the efforts.... I have one query, how many more videos will this series going to have.. ?
Bhaiya in beginning itself you said it is a hard problem let's see if I'll be understand it or not
I think we should simply create a graph from the given tree and thereby the bfs will be bit more easy. That's the way I solved this question.
please someone give me the code links
provided link has links only till lecture 65
save trees !! save nature !! save lives 🙃
Thankyou so much Bhaiya. Can't be more thankful !
Loving the series bhaiya... Sab samajh aa rha h
Bhaiya, es lacture ka code github pe nahi hai.. please upload kar dijiye.
bohot badiya question maja aa gya! wah babbar ji wah
Here is the code with some minor updates :
class Solution {
private:
Node* createParentMapping(Node* root, int target, map& nodeToParent) {
Node* res = nullptr;
queue q;
q.push(root);
nodeToParent[root] = nullptr;
while (!q.empty()) {
Node* front = q.front();
q.pop();
if (front->data == target) {
res = front;
}
if (front->left) {
nodeToParent[front->left] = front;
q.push(front->left);
}
if (front->right) {
nodeToParent[front->right] = front;
q.push(front->right);
}
}
return res;
}
int burnTree(Node* root, map& nodeToParent) {
if (!root) {
return 0;
}
unordered_map visited;
queue q;
q.push(root);
visited[root] = true;
int ans = 0;
while (!q.empty()) {
int size = q.size();
bool flag = false;
for (int i = 0; i < size; i++) {
Node* front = q.front();
q.pop();
if (front->left && !visited[front->left]) {
flag = true;
q.push(front->left);
visited[front->left] = true;
}
if (front->right && !visited[front->right]) {
flag = true;
q.push(front->right);
visited[front->right] = true;
}
if (nodeToParent[front] && !visited[nodeToParent[front]]) {
flag = true;
q.push(nodeToParent[front]);
visited[nodeToParent[front]] = true;
}
}
if (flag) {
ans++;
}
}
return ans;
}
public:
int minTime(Node* root, int target) {
map nodeToParent;
Node* targetNode = createParentMapping(root, target, nodeToParent);
int ans = burnTree(targetNode, nodeToParent);
return ans;
}
};
Congratulations for 125K family 🤩🥳🥳🥳
Mehnat he aapki 🤍
🚨🚨🚨🚨
Bhaiya plz make a Roadmap For InternShip 🥺 with DSA busted Course as resource 🤩
Thank You So Much BHRATA SHREE !!!!!!!
iska b code nhi hai bhaiyaa github pe yahase aage ke lecture nhi he plz krdo upload
very nice bhaiyaa everything is understood...... bit tricky but after practicing 2 or three times it would be easy to impliment and understand ...thanks bhaiyaa... keep it up!! your dedication is inspiration to all of us...!!!
Jabardast bhaiya 👍👍
You made the Hard problem easy, Thanks a lot
Here, you solved using O(N) space complexity, but in question, we require O(H) space complexity.
Sahi questions tha bhai maja aa gaya solve karne ka.
Best solution for this problem 👍🏻♥️🙏
Very nice bhaiya but for only typing speed dont write the misspelled word again and again, it leads to distraction.
bhaiya apke is video ne toh dil jeet liya thankyou for this awesome series ;
bhaijaan please yhi c++ ke tarah 1 JAVA ka placement dsa course le aaye WITHOUT ANY ONE SHOT on youtube
Salute You Bhaiya💯💯💯
hnji this is anurag patel
Lec 67 completed successfully ✅
Commenting for better reach!!
thanks a lot bhaiya
Hello Bhaiya ❤️😍😘🥰😘🥰.... Aa gya hu.....
Ab to college khul chuka hai... Abhi college me hu
Thanks bhaiya ❤️❤️😍😍😍😍😍😍
Bhaiya ham apko nam roshan krege ❤️
Thank you bhaiya for your efforts and also your explanation is the BEST❤
Front-left=front how will this create map between two nodes😢
Learnt coding, now need to learn the intiution myself
P.S: Coded on my own.
Bhai Bhai Bhai consistency oppppoo
class Solution {
public:
void getAdjacents(Node*root,Node*parent,unordered_map&mp)
{
if(!root)
{
return;
}
vectortemp;
if(parent!=NULL)
{
temp.push_back(parent->data);
}
if(root->left)
{
temp.push_back(root->left->data);
}
if(root->right)
{
temp.push_back(root->right->data);
}
mp[root->data]=temp;
getAdjacents(root->left,root,mp);
getAdjacents(root->right,root,mp);
}
int minTime(Node* root, int start)
{
// Your code goes here
unordered_mapmp;
getAdjacents(root,NULL,mp);
unordered_mapvis;
queueq;
int count=0;
q.push(start);
while(!q.empty())
{
int size=q.size();
for(int i=0;i
i code this problem after watching just your algo
boht mazza aaya pehle toh lga nhi ho paega pher yaad aaya -Just ask 1 question “ Why I started ?
I also try to take that line to motivate me but that just doesn't work for me.Can you please tell what's that keeps you motivated or I should say why you started.
Bhaiya aap great ho I have no words ♥️♥️ next level 😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘
Here is a single function C++ code
class Solution {
public:
int minTime(Node* root, int target)
{
if(root==NULL)
{
return 0;
}
map node_to_parent;
queue q;
q.push(root);
node_to_parent[root] = NULL;
Node* ans = NULL;
while(!q.empty())
{
Node* temp = q.front();
q.pop();
if(temp->data == target)
{
ans = temp; // target is found
}
if(temp->left != NULL)
{
node_to_parent[temp->left] = temp;
q.push(temp->left);
}
if(temp->right!=NULL)
{
node_to_parent[temp->right] = temp;
q.push(temp->right);
}
}
// ans is found
map visited;
visited[ans] = true;
int time = 0;
queue q1;
q1.push(ans);
while(!q1.empty())
{
int size = q1.size();
bool flag = false;
for(int i=0;ileft!=NULL && visited[t1->left]!=1)
{
flag = true;
visited[t1->left] = true;
q1.push(t1->left);
}
if(t1->right!=NULL && visited[t1->right]!=1)
{
flag = true;
visited[t1->right] = true;
q1.push(t1->right);
}
if(node_to_parent[t1]!=NULL && visited[node_to_parent[t1]]!=1)
{
flag = true;
visited[node_to_parent[t1]] = true;
q1.push(node_to_parent[t1]);
}
}
if(flag)
{
time++;
}
}
return time;
}
};
done bhaiya
class Solution {
private:
unordered_map node2Parent;
unordered_map visited;
queueq;
Node* targetNode = NULL;
int time = 0;
//step 1: create node 2 parent mapping via level order traversal
//and also try to find target node
void getNodeToParent(Node* root, int target){
queueaux;
aux.push(root);
while(!aux.empty()){
Node* curr = aux.front();
aux.pop();
//checking if current node is target node
if(curr->data == target){
targetNode = curr;
}
//set parent of root to null
if(curr==root){
node2Parent[root]=NULL;
}
if(curr->left){
node2Parent[curr->left] = curr;
aux.push(curr->left);
}
if(curr->right){
node2Parent[curr->right] = curr;
aux.push(curr->right);
}
}
}
void burning(){
while(!q.empty()){
Node* curr = q.front();
q.pop();
if(curr==NULL){
time++;
if(!q.empty()){
q.push(NULL);
}
}
else{
visited[curr]=true;
// Check for left node
if(curr->left && !visited[curr->left]){
q.push(curr->left);
}
// Check for right node
if(curr->right && !visited[curr->right]){
q.push(curr->right);
}
// Check for parent node
Node* parent = node2Parent[curr];
if(parent && !visited[parent]){
q.push(parent);
}
}
}
}
public:
int minTime(Node* root, int target)
{
getNodeToParent(root,target);
q.push(targetNode);
q.push(NULL);
burning();
return time-1;
}
};
Thank you bhaiya..
Consistency ++
Consistency....Prathishta...Anushaashan....
bhaiya codes k link? github pr apne aage k nhi upload kiye
Baaki sb to theek hai, but you never taught people debugging!
bhaiya iske source code kaha hai???? github pe nahi dikh raha hai
26:27 😁😁😂😂 kyu tum log ne vahi suna jo mene sunna ?
Thanks brother for such amazing contenet
better than striver's explanation
happy married life to you bhaiya. 😁😁😁😁😂😂😂😂
full code :)
class Solution {
public:
//step-1 create mapping and return the target node
Node* createParentMapping(Node* root,int target,map &nodeToParent){
Node* res = NULL; //to store the result;
queue q;
q.push(root);
nodeToParent[root] = NULL;
while(!q.empty()){
Node* front = q.front();
q.pop();
if(front->data==target){
res = front;
}
if(front->left){
nodeToParent[front->left] = front;
q.push(front->left);
}
if(front->right){
nodeToParent[front->right] = front;
q.push(front->right);
}
}
return res;
}
int burnTree(Node* root,map &nodeToParent){
map visited;
queue q;
q.push(root);
visited[root] = true;
int ans = 0;
while(!q.empty()){
bool flag = 0;
int size = q.size();
for(int i=0;ileft && !visited[front->left]){
flag = 1;
q.push(front->left);
visited[front->left] = 1;
}
if(front->right && !visited[front->right]){
flag = 1;
q.push(front->right);
visited[front->right] = 1;
}
if(nodeToParent[front] && !visited[nodeToParent[front]]){
flag = 1;
q.push(nodeToParent[front]);
visited[nodeToParent[front]] = 1;
}
}
if(flag==1){
ans++;
}
}
return ans;
}
int minTime(Node* root, int target)
{
map nodeToParent;
Node* targetNode = createParentMapping(root,target,nodeToParent);
int ans = burnTree(targetNode,nodeToParent);
return ans;
}
};
Amazing bhiaya 🙏
We will make you proud
bhai code ki link nahi daali kya github me?
Present bhaiya❤
You are really doing great for us bhaiya ❤❤❤
Great 😃👍👍👍 fantastic
learning from the best!
Attandance marked thankyou
Que1 Bhaiya jb pointers pd rhe the tb aapne btaya tha ki pointer ko ( int* p;) is tarike se initialise krna bad practice h kyoki isme koi garbage value aa jaegi jo shayad koi confidential data bhi ho skta h... Lekin linked list ke implementation me fir (Node* next;) ye kyu initialise kiya?????????
Que2 insertion at last node kr time bhi last node ke pointer ko null se hta kr new node ko point krvate h, lekin pointer me dekha tha ki null vector ki value change nhi kr skte????
Que3 jese array ke case me *arr krte the to uska data milta tha vese linked list me kisi bhi next ko dereference krke yani *next krke access kre to kya next node ka data milega???
Que 4 kya mujhe pointer vapas revise krna chahiye 🤣🤣
Ans1 we have not initialised it by (Node * next); it's just class structure saying that the object of this class will have this property called next which is pintor of node....we later initialised it in constructor making it NULL like (this->next = NULL) at this time we are initialising it.not earlier...
Ans2 --> we cannot excess data of null vector but we can always change the direction of null vector means When we are adding a node at the end of LL, we are changing last node's next vector from NULL to the new Node (node's address as next is a pointor of node so we can only save address in it )
@@pradipsinhdarbarofficial2098 thanks bro.. 3rd question bhi check krke dekho please mere pas laptop ya pc nhi h
@@pradipsinhdarbarofficial2098 Thanx for the explanation
labrador traversal🤣🤣🤣🤣. btw noicely explained
class Solution {
private:
TreeNode*f(TreeNode* root, int start,map&mpp){
mpp[root]=NULL;
TreeNode*target=NULL;
queueq;
q.push(root);
while(!q.empty()){
TreeNode*front=q.front();
q.pop();
if(front->val==start){
target=front;
}
if(front->right){
mpp[front->right]=front;
q.push(front->right);
}
if(front->left){
mpp[front->left]=front;
q.push(front->left);
}
}
return target;
}
int g(TreeNode*target,map&mpp){
mapvis;
vis[target]=1;
queueq;
int ans=0;
q.push(target);
while(!q.empty()){
int size=q.size();
bool flag=0;
for(int i=0;iright && !vis[front->right]){
flag=1;
vis[front->right]=1;
q.push(front->right);
}
if(front->left && !vis[front->left]){
flag=1;
vis[front->left]=1;
q.push(front->left);
}
if(mpp[front] && !vis[mpp[front]]){
flag=1;
vis[mpp[front]]=1;
q.push(mpp[front]);
}
}
if(flag==1){
ans+=1;
}
}
return ans;
}
public:
int amountOfTime(TreeNode* root, int start) {
mapmpp;
TreeNode*target=f(root,start,mpp);
int ans=g(target,mpp);
return ans;
}
};
Thanks bhaiya for easy and nice explanation👏
Words aren't enough bade bhaiya 🙏
why take Visited map instead of visited array???
Attendance marked
Maza hi agya bhaiyya🤩🤩🔥🔥
best content ever...
Shandar bhaiya
bohot bohot shukriya