Lecture 67: Minimum Time to BURN the Entire Binary Tree || C++ Placement Series

  Рет қаралды 122,475

CodeHelp - by Babbar

CodeHelp - by Babbar

Күн бұрын

Пікірлер: 229
@govindsuryavanshi6653
@govindsuryavanshi6653 Жыл бұрын
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.
@its_nawed4611
@its_nawed4611 Жыл бұрын
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.
@invinishku1542
@invinishku1542 2 жыл бұрын
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..
@harshpandey4190
@harshpandey4190 Жыл бұрын
You should be Appreciated for explaining the toughest sum with such a ease
@verma_jay
@verma_jay Жыл бұрын
Your explanation was this good that I coded the solution myself by just seeing the algorithm🔥
@pkyadav6230
@pkyadav6230 Жыл бұрын
Really??
@Ash_9202
@Ash_9202 Жыл бұрын
​@@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.
@yashchauhan7221
@yashchauhan7221 2 жыл бұрын
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 🔥🔥
@kumarpawansahh
@kumarpawansahh 2 жыл бұрын
Sabd kamm parr jaa rhe hai aapki dedication dekh kr ❤️
@shashanksingh4708
@shashanksingh4708 2 жыл бұрын
I was able to write the code myself after watching the first half of the video. Thanks for the awesome content!
@anshumansharma1069
@anshumansharma1069 Жыл бұрын
To be honest bhtt aasan laga jaise apne sikhaya bhaiyaa💖💖
@akshithabharde05
@akshithabharde05 3 күн бұрын
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
@janvikundnani9203
@janvikundnani9203 2 жыл бұрын
Absolutely love the way you make these hard problems seem so simple!! Thank you so much for such efforts!!🔥
@RohitKumar-oo8lo
@RohitKumar-oo8lo 2 жыл бұрын
Agree
@RohitKumar-oo8lo
@RohitKumar-oo8lo 2 жыл бұрын
Dhamaal machaa rha hai Course bhaiya!!!
@AA-wy9vw
@AA-wy9vw 2 жыл бұрын
Loving the series bhaiya! Aapki consistency hume inspire karti hai aur mehnat marne ke liye
@mr.himanshu4299
@mr.himanshu4299 2 жыл бұрын
thoda piche reh gya hun pr dheere dheere cover kr rha hun end takk complete krunga course ko keep uploading babbar bhai
@aqusatabassumsyedsadiqueal5920
@aqusatabassumsyedsadiqueal5920 2 жыл бұрын
You're continuing despite less views in later vedios , 👏👏👏
@Shyara2308
@Shyara2308 Жыл бұрын
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
@ramanjaneyuluv479
@ramanjaneyuluv479 11 ай бұрын
babbar bhayya is love 💝
@prashantshukla6870
@prashantshukla6870 2 жыл бұрын
I can say today😜 Maine toh aag lga di binary tree ko 😂😂😂😂
@lazyemperor5182
@lazyemperor5182 Жыл бұрын
🔥🔥🔥🔥
@keshavgambhir9894
@keshavgambhir9894 2 жыл бұрын
Present bhaiya Consistency op 🔥🔥🔥🔥 Commenting for a better reach
@utkarshsingh1457
@utkarshsingh1457 2 жыл бұрын
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!!!
@pranayavnish8028
@pranayavnish8028 Жыл бұрын
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.
@Ash_9202
@Ash_9202 Жыл бұрын
​@@pranayavnish8028tbhi itna lamba solution bana hai Par kar bhi kya skte hai jb tk graphs pe na poch jaye hum
@pranayavnish8028
@pranayavnish8028 Жыл бұрын
@@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.
@Ash_9202
@Ash_9202 Жыл бұрын
@@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.
@JungleKiKahaniyan8
@JungleKiKahaniyan8 2 жыл бұрын
Maja aa raha hai Aapki consistency ++
@dipakkumarsingh.7151
@dipakkumarsingh.7151 3 ай бұрын
Aaj to aag lagadi bhai. dhuwa dhuwa kardiya binary tree ka
@Nishant-n8e
@Nishant-n8e 10 ай бұрын
i did with pair function
@MohitKumar-ys8jd
@MohitKumar-ys8jd Жыл бұрын
Your explanation is the best!! Thanks for this amazing playlist!!
@shubhamrao5952
@shubhamrao5952 Жыл бұрын
This question was asked by me 2 years back in Paytm SDE 1 interview.
@KPCoder
@KPCoder 2 жыл бұрын
Thanks you sir, excellent content, no one can explain like you from basic 🙏 god bless you
@crazyduniya128
@crazyduniya128 2 жыл бұрын
bohot badiya question maja aa gya! wah babbar ji wah
@code_sleep_live
@code_sleep_live 5 ай бұрын
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; } };
@ashurajput3526
@ashurajput3526 6 ай бұрын
best explanation for burning tree on utube
@mriduljain6809
@mriduljain6809 2 жыл бұрын
Loving the series bhaiya... Sab samajh aa rha h
@sachinrathod8319
@sachinrathod8319 2 жыл бұрын
Sahi questions tha bhai maja aa gaya solve karne ka.
@vivek25467
@vivek25467 2 ай бұрын
save trees !! save nature !! save lives 🙃
@RohitKumar-lt2bo
@RohitKumar-lt2bo 7 ай бұрын
TC: O(n) //when unordered_map is used insted of map SC: O(n)
@Phantomex984
@Phantomex984 Жыл бұрын
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; } };
@RahulKumar-wt9qc
@RahulKumar-wt9qc 2 жыл бұрын
Jabardast bhaiya 👍👍
@amishapandey6875
@amishapandey6875 Жыл бұрын
Thankyou so much Bhaiya. Can't be more thankful !
@rachit_joshi
@rachit_joshi 7 ай бұрын
Thank You So Much BHRATA SHREE !!!!!!!
@SauravKumar-yp1fy
@SauravKumar-yp1fy 2 жыл бұрын
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
@madetolaugh3476
@madetolaugh3476 2 жыл бұрын
Consistency....Prathishta...Anushaashan....
@shashikantverma9931
@shashikantverma9931 2 жыл бұрын
Bhai Bhai Bhai consistency oppppoo
@suyashjain3223
@suyashjain3223 Жыл бұрын
You made the Hard problem easy, Thanks a lot
@monubhargav346
@monubhargav346 Жыл бұрын
space complexity is becoming n but allowed space complexity is O(height of tree)
@parikshitgupta7711
@parikshitgupta7711 2 жыл бұрын
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
@ishansingh6241
@ishansingh6241 10 ай бұрын
I am thinking mai ise adjancy list bana kr bsf chala dun usse bhi ho jayega sayad.
@suranjandhara7535
@suranjandhara7535 2 жыл бұрын
Hello Bhaiya ❤️😍😘🥰😘🥰.... Aa gya hu..... Ab to college khul chuka hai... Abhi college me hu Thanks bhaiya ❤️❤️😍😍😍😍😍😍
@tusharvaish8096
@tusharvaish8096 2 жыл бұрын
bhaiya apke is video ne toh dil jeet liya thankyou for this awesome series ;
@daipayannit-s415
@daipayannit-s415 Жыл бұрын
Commenting for better reach!!
@unboxtheuniverse5336
@unboxtheuniverse5336 2 жыл бұрын
Congratulations for 125K family 🤩🥳🥳🥳 Mehnat he aapki 🤍 🚨🚨🚨🚨 Bhaiya plz make a Roadmap For InternShip 🥺 with DSA busted Course as resource 🤩
@prikshit_sharma4071
@prikshit_sharma4071 2 жыл бұрын
Bhaiya ham apko nam roshan krege ❤️
@shivkumar-og4ow
@shivkumar-og4ow 2 жыл бұрын
Thank you bhaiya.. Consistency ++
@azaankhan5208
@azaankhan5208 2 жыл бұрын
Superbbbbb !!! Thank you so much for all the efforts.... I have one query, how many more videos will this series going to have.. ?
@trojanhorse8278
@trojanhorse8278 10 ай бұрын
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.
@SirAkPandey
@SirAkPandey Жыл бұрын
Best solution for this problem 👍🏻♥️🙏
@tejashmaurya2149
@tejashmaurya2149 Жыл бұрын
Salute You Bhaiya💯💯💯
@naveensingh_0735
@naveensingh_0735 2 жыл бұрын
Words aren't enough bade bhaiya 🙏
@prashantbirajdar9271
@prashantbirajdar9271 2 жыл бұрын
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...!!!
@naveensingh_0735
@naveensingh_0735 2 жыл бұрын
We will make you proud
@dwivedi6037
@dwivedi6037 2 жыл бұрын
Bhaiya aap great ho I have no words ♥️♥️ next level 😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘😘
@Bhagwati_Jewellers.
@Bhagwati_Jewellers. 2 жыл бұрын
Amazing bhiaya 🙏
@jenithpatel5594
@jenithpatel5594 2 жыл бұрын
Here, you solved using O(N) space complexity, but in question, we require O(H) space complexity.
@letsstartwithrahul8841
@letsstartwithrahul8841 Жыл бұрын
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
@Vishal-cm4dl
@Vishal-cm4dl Жыл бұрын
Yeah, simply we can increment time every time when !q.empy()
@anshikasingh4158
@anshikasingh4158 Жыл бұрын
boht mazza aaya pehle toh lga nhi ho paega pher yaad aaya -Just ask 1 question “ Why I started ?
@ihateracistandblackpeople4272
@ihateracistandblackpeople4272 Жыл бұрын
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.
@RohitSingh-hc8yi
@RohitSingh-hc8yi Жыл бұрын
thanks a lot bhaiya
@anuragpatel7787
@anuragpatel7787 9 ай бұрын
hnji this is anurag patel Lec 67 completed successfully ✅
@ravivishvas3676
@ravivishvas3676 2 жыл бұрын
best content ever...
@sherebanoburhani8939
@sherebanoburhani8939 2 жыл бұрын
Present bhaiya❤
@sumitchakrabortystudy651
@sumitchakrabortystudy651 2 жыл бұрын
labrador traversal🤣🤣🤣🤣. btw noicely explained
@sukhpreetsingh5200
@sukhpreetsingh5200 2 жыл бұрын
done bhaiya
@abhishekshimpi214
@abhishekshimpi214 Жыл бұрын
Maza hi agya bhaiyya🤩🤩🔥🔥
@tanishkayadav7351
@tanishkayadav7351 2 жыл бұрын
learning from the best!
@vivekrathore6030
@vivekrathore6030 Жыл бұрын
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; } };
@afeefuddin8883
@afeefuddin8883 Жыл бұрын
Learnt coding, now need to learn the intiution myself P.S: Coded on my own.
@ayushp4471
@ayushp4471 2 жыл бұрын
Thank you bhaiya for your efforts and also your explanation is the BEST❤
@deepak44222
@deepak44222 Жыл бұрын
happy married life to you bhaiya. 😁😁😁😁😂😂😂😂
@AbhishekGupta-tn8sp
@AbhishekGupta-tn8sp Жыл бұрын
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; } };
@anuptewary3016
@anuptewary3016 2 жыл бұрын
Present bhaiya with lot's of fire 🔥
@yashchampaneri1972
@yashchampaneri1972 2 жыл бұрын
26:27 😁😁😂😂 kyu tum log ne vahi suna jo mene sunna ?
@zishan53
@zishan53 2 жыл бұрын
Great 😃👍👍👍 fantastic
@bheemsingh-fh1zu
@bheemsingh-fh1zu 2 жыл бұрын
Present bhaiya Reach++😊
@muhammadsuleman8936
@muhammadsuleman8936 Жыл бұрын
Thanks brother for such amazing contenet
@DivyanshuYadav-t7l
@DivyanshuYadav-t7l 7 ай бұрын
better than striver's explanation
@kalpeshmali1476
@kalpeshmali1476 2 жыл бұрын
Shandar bhaiya
@vinayaksinghal
@vinayaksinghal 10 ай бұрын
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; } };
@divyamvijay9071
@divyamvijay9071 Жыл бұрын
You are really doing great for us bhaiya ❤❤❤
@aryankr
@aryankr 7 ай бұрын
bohot bohot shukriya
@jericodcosta2049
@jericodcosta2049 2 жыл бұрын
Attandance marked thankyou
@codingaid2753
@codingaid2753 2 жыл бұрын
Thanks bhaiya for easy and nice explanation👏
@RohitRana-tz2lr
@RohitRana-tz2lr 2 жыл бұрын
Thanks bhaiya for all of your efforts
@aditichourasia2990
@aditichourasia2990 2 жыл бұрын
Hats off!
@RohitSingh-hc8yi
@RohitSingh-hc8yi Жыл бұрын
Bhaiya in beginning itself you said it is a hard problem let's see if I'll be understand it or not
@mritunjay7065
@mritunjay7065 2 жыл бұрын
Awesome explanation !👏
@srajittanwar636
@srajittanwar636 2 жыл бұрын
Present Bhaiya
@torus151
@torus151 2 жыл бұрын
present sirji
@vinaysiwach9734
@vinaysiwach9734 9 ай бұрын
i code this problem after watching just your algo
@RohitSingh-hc8yi
@RohitSingh-hc8yi Жыл бұрын
Attendance marked
@sunny-gb6xh
@sunny-gb6xh Жыл бұрын
thank you bhaiya for your grt content :)
@KratosProton
@KratosProton 2 жыл бұрын
Great Explaination!
@devangmishra4506
@devangmishra4506 2 жыл бұрын
maza la diya bhaiya ne
@APOORVSAHU-ro6vc
@APOORVSAHU-ro6vc 2 жыл бұрын
I watch full video Thanks 🙏
@sainikkhaddar6064
@sainikkhaddar6064 2 жыл бұрын
GOD LEVEL SIR
@BhaiRayyanKiDuniya
@BhaiRayyanKiDuniya 2 жыл бұрын
15:51 minTime ()
L31. Minimum time taken to BURN the Binary Tree from a Node | C++ | Java
18:24
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 835 М.
Lecture 66: Construct a Binary Tree from InOrder/PreOrder/PostOrder Traversal
36:35
Lecture 65: Binary Tree FAANG Interview Questions || Part-3
59:15
CodeHelp - by Babbar
Рет қаралды 276 М.
Lecture 82: Rat in Maze Problem || Backtracking Day 1 || C++ Placement Series
31:37
Lecture 73: Largest BST in a Binary Tree || C++ Placement Series
23:09
CodeHelp - by Babbar
Рет қаралды 87 М.
Why is Python 150X slower than C?
10:45
Mehul - Codedamn
Рет қаралды 16 М.