Burn a Tree | Minimum Time To Burn A Binary Tree Starting From The Leaf Node | DSA-One Course #65

  Рет қаралды 33,759

Anuj Bhaiya

Anuj Bhaiya

Күн бұрын

Hey guys, In this video, We're going to solve an interesting problem called: Minimum Time To Burn A Binary Tree Starting From The Leaf Node.
Use the code ANUJBHAIYA on GeeksforGeeks to avail discount on courses.
🥳 Join our Telegram Community:
Telegram channel: telegram.me/realanujbhaiya
Telegram group: telegram.me/dsa_one
Follow me on:
Instagram: / anuj.kumar.sharma
Linkedin: / sharma-kumar-anuj
Twitter: / realanujbhaiya
Complete DSA Playlist:
DSA-One course: • Time Complexity & Big ...
Complete Android Development Playlist: • Android Development Tu...
Hashtags:
#anujbhaiya #dsaone
Tags:
burn a tree
anuj bhaiya
burning tree
burn a binary tree
anuj bhaiya java
minimum cost tree from leaf values
binary tree in data structure
binary tree
burn tree
burning tree gfg
apna college
burn a tree interviewbit
burn binary tree
java by anuj bhaiya
time to burn
tree dsa
tree in data structure
akshay saini
anuj
love babbar
minimum depth of binary tree
minimum time to burn a tree
threaded binary tree in data structure
tree
tree algorithms
tree in dsa
anuj bhaiya dsa
anuj dsa
anuj kumar sharma
apna college python
binary tree c++
binary tree in java
binary tree playlists
boundary of binary tree
boundary traversal of binary tree
bst iterator
burning trees
coding ninjas
depth of binary tree
diameter of binary tree
dsp anuj bhaiya
flattening a linked list
game theory
geeksforgeeks
graph java
inorder traversal binary tree
java anuj bhaiya
kunal kushwaha
maximum path sum in binary tree
news views & updates
node
prim's algorithm for minimum spanning trees
seed it solution
top view of binary tree
tree burning
tree in python
trees in dsa

Пікірлер: 71
@harshabhardwaj7525
@harshabhardwaj7525 2 жыл бұрын
why your course “JAVA| DSA + ALGO” got removed from apni kaksha?? i was following that course and was not able to complete it cz it got removed. can u please tell me where can i find it?
@sahilhansofficial
@sahilhansofficial 2 жыл бұрын
Sir android studio waali playlist ke last app pe hu uske baad kha se practice kr skte h? Ya koi course krna chahiye, pls suggest😊
@RiteshSingh-rz7tz
@RiteshSingh-rz7tz 2 жыл бұрын
Thanks for continuing this series. I am addicted to it.
@laveshgarg2189
@laveshgarg2189 2 жыл бұрын
Thanku Bhaiya for the amazing consistency in dsa one course
@aishwaryshukla8880
@aishwaryshukla8880 Жыл бұрын
Bhaiya this code is not working on gfg practice! I modified the code so that it handles when we burn from the root node itself but still a test case is failing where the tree is skewed. PLEASE DO CORRECTION 🙏🏻🛑⭕🛑⭕🛑⭕🛑⭕
@abhinavvarshney6366
@abhinavvarshney6366 2 жыл бұрын
.... THANK YOU ANUJ BHAIYA....
@nittinrana2277
@nittinrana2277 2 жыл бұрын
Bahiya thanks for maintaining consistency
@laveshgarg2189
@laveshgarg2189 2 жыл бұрын
Bhaiya the approach was mind blowing maza aa gaya Thanku Bhaiya
@reenayadav8468
@reenayadav8468 2 жыл бұрын
Thank you sir
@abacas2175
@abacas2175 2 жыл бұрын
First comment... Bhayiya java complete course daliya please..i like ur way of teaching!!!!
@Lifeofkashh31
@Lifeofkashh31 2 жыл бұрын
agya samjh rhanks a lot
@pritamsarkar3371
@pritamsarkar3371 2 жыл бұрын
1st traverse the tree get all parents in a hash map , 2nd find that node, from where to start, 3rd apply bfs including left child, right child, and parent then get the maximum level from that, that will be the burning time, Thank u anuj Bhai
@pritamsarkar3371
@pritamsarkar3371 2 жыл бұрын
1st and second step can be done together using a global node type variable
@sdwivedi
@sdwivedi 2 жыл бұрын
Thanks bhaiya
@hamzaansari5714
@hamzaansari5714 2 жыл бұрын
Thank you bhai
@sarangavadiya7087
@sarangavadiya7087 2 жыл бұрын
thank you bhaiya.
@saritaprasad4295
@saritaprasad4295 Жыл бұрын
Thanks
@subhamhazracreation5634
@subhamhazracreation5634 2 жыл бұрын
Bhaiya plz apni kaksha k java playlist ko unhide karne k liye kahiye...atleast for a day download kar lunga.... 39 videos dekha tha uske baad sab hide kar diya gaya hai.... plz bhaiya.... yaha aapka dsa ka series kar raha hu...aur waha java kar raha tha.....
@Sanyamjain77
@Sanyamjain77 Жыл бұрын
Thanks for this solution, Actually in yesterday's weekly contest this problem - "Amount of time for binary tree to get infected" came and I was not able to solve it. All the other channels were providing the (tree -> graph then bfs) solution. But, I wanted to solve the problem using recursion and simple tree traversal. Your video really helped me. Thanks a lot for your efforts.
@rohandikshit2171
@rohandikshit2171 Жыл бұрын
Can you share that code, I am having some issues in getting all the test cases passed.
@Sanyamjain77
@Sanyamjain77 Жыл бұрын
@@rohandikshit2171 Yeah sure, here it is: class Solution { static int ans; class Depth { int d; Depth(int d){ this.d = d; } } int height(TreeNode node){ if(node == null) return 0; int lh = height(node.left); int rh = height(node.right); return Math.max(lh, rh) + 1; } int helper(TreeNode node, int target, Depth depth){ if(node == null) return 0; if(node.val == target){ depth.d = 0; int lh = height(node.left); int rh = height(node.right); ans = Math.max(lh, rh); return 0; } Depth ld = new Depth(-1); Depth rd = new Depth(-1); int lh = helper(node.left, target, ld); int rh = helper(node.right, target, rd); if(ld.d > -1){ ans = Math.max(ld.d + 1 + rh, ans); depth.d = ld.d + 1; }else if(rd.d > -1){ ans = Math.max(rd.d + 1 + lh, ans); depth.d = rd.d + 1; } return Math.max(lh, rh) + 1; } public int amountOfTime(TreeNode root, int start) { helper(root, start, new Depth(-1)); return ans; } }
@rohandikshit2171
@rohandikshit2171 Жыл бұрын
@@Sanyamjain77 Thanks👍
@Sanyamjain77
@Sanyamjain77 Жыл бұрын
@@rohandikshit2171 You're welcome!
@jagankumarpatra
@jagankumarpatra Жыл бұрын
@@Sanyamjain77 galat he code gfg me chal nehi raha he
@keshavkamalkarn1431
@keshavkamalkarn1431 2 жыл бұрын
Thankyou bhai for your guidance and Bhaiya Is data structure and algorithm different for different languages?
@warriorgaming9509
@warriorgaming9509 Жыл бұрын
NO
@aryanvarma2536
@aryanvarma2536 2 жыл бұрын
Hello bhaiya
@user-eq1kt3js6z
@user-eq1kt3js6z 5 ай бұрын
Can we use greedy by taking the node which in the centre of the most depth path
@annanyavijay4267
@annanyavijay4267 2 жыл бұрын
Can we use static depth variable instead of making it a wrapper class?
@sagarchawla4926
@sagarchawla4926 Жыл бұрын
Did you get the answer?
@sadavarshsaini9801
@sadavarshsaini9801 2 жыл бұрын
Bro m jab bhi kisi website se koi question karta hu tab mujhe question hi samjh nhi aata ki kya pucha h jab ki code mujhe aata rhta h but question nhi samjh pata kya pucha h isliye kbhi Kar nhi pata please guide me
@Lifeofkashh31
@Lifeofkashh31 2 жыл бұрын
bhiya ye samajh ni aya kya karu@anuj bhiya can u explain why we are having static class sepaerately for that depth
@bharatDarshaByDrone
@bharatDarshaByDrone 2 жыл бұрын
Felling First With 6 others
@ashwiniskumar4275
@ashwiniskumar4275 2 жыл бұрын
Will this work only for burn from leaf node or burn from any target node.
@yashyadav7927
@yashyadav7927 Жыл бұрын
same question ... bhaiya if u there plz reply us
@sudhanshudubey7267
@sudhanshudubey7267 2 жыл бұрын
bhaiya please make video on hackathons
@laplinanayak7688
@laplinanayak7688 2 жыл бұрын
how we can install jdk in chrombook please upload vedio on this topic
@mrrishiraj88
@mrrishiraj88 2 жыл бұрын
🙏👍👍
@subhankarpal2800
@subhankarpal2800 2 жыл бұрын
Sir I did this code on GFG Practice ,I understand whole logic after that I tried by my own but many times I got different result but now I wrote code as same as in your video but now still got different and , did not run any test case please help me sir , This is the Code Output --------------------------------------- 1 2 3 4 5 N 6 N N 7 8 N 9 10 11 N N N 12 N N N 13 8 Your Output: 8 Expected Output: 7 Main code ----------------------- static class Depth{ int d; public Depth(int d){ this.d = d; } } public static int minTime(Node root, int target) { // Your code goes here Depth depth = new Depth(-1); burn(root,target,depth); return ans; } static int ans = -1; public static int burn(Node root,int target,Depth depth){ if(root == null)return 0; if(root.data == target){ depth.d = 1; return 1; } Depth ld = new Depth(-1); Depth rd = new Depth(-1); int lh = burn(root.left,target,ld); int rh = burn(root.right,target,rd); if(ld.d != -1){ ans = Math.max(ans,ld.d+1+rh); depth.d = ld.d+1; } else if(rd.d != -1){ ans = Math.max(ans,rd.d+1+lh); depth.d = rd.d+1; } return Math.max(lh,rh)+1; }
@geetikabhatnagar8113
@geetikabhatnagar8113 Жыл бұрын
int burn(Node* root,int target,int &d){ if(root==NULL) return 0; int ld=-1; int rd=-1; int lh=burn(root->left,target,ld); int rh=burn(root->right,target,rd); if(ld!=-1){ ans=max(ans,ld+1+rh); d=ld+1; } else if(rd!=-1){ ans=max(ans,rd+1+lh); d=rd+1;} if(root->data==target){ d=0; //start depth from 0 ans=max(ans,max(lh,rh)); //add this for burn from any node return 0; }
@pranavmittal8249
@pranavmittal8249 2 жыл бұрын
Bhaiya ek video git commands ke uper bhi bna 😣
@pkyadav6230
@pkyadav6230 Жыл бұрын
7:24 that's ampersand ' &'
@rhinoara7119
@rhinoara7119 2 жыл бұрын
Bhiya ek doubt, code meh ek 100 size ka array difine Kare tho vo constant space complexity mane jayega?
@tusharsharma6712
@tusharsharma6712 2 жыл бұрын
no
@sagarchawla4926
@sagarchawla4926 Жыл бұрын
Yes
@payaskakar3938
@payaskakar3938 2 жыл бұрын
Wonder how people come up with the approach in the interview, if they hadn't solved it before.😶
@k9killer
@k9killer 2 жыл бұрын
no one bro , and interview me standard questions hi puche jate hai , so worry not
@ankushaviraj8292
@ankushaviraj8292 2 жыл бұрын
Bhaiya Java ka full course apni kasha pe show nahi hora hai kaha se dekhe
@arpanmaheshwari2290
@arpanmaheshwari2290 2 жыл бұрын
First
@chinmaychaudhari4143
@chinmaychaudhari4143 2 жыл бұрын
Bhaiya topic wise dsa ki ek question sheet banva do na ... That would help us lot to practice while learning dsa...
@AjitSingh-rg3zu
@AjitSingh-rg3zu Жыл бұрын
Sir, the solution you gave is working only for the cases in which burning of tree starts from leaf node. Could you please recheck it and confirm if the solution will run for all cases or not
@akshaypatil8940
@akshaypatil8940 2 ай бұрын
I have modified it to make it right.
@akshaypatil8940
@akshaypatil8940 2 ай бұрын
If (target == root.data) { Int height = Math.max (burnTree(root.right), burnTree(root.left)); ans = Math.max(ans, height); return 1; }
@rajeevshetty8929
@rajeevshetty8929 Ай бұрын
@@akshaypatil8940 return ans
@priyanshumohanty5261
@priyanshumohanty5261 2 жыл бұрын
1:16 Poora ka poora tree swaha 😂
@mdnayab5839
@mdnayab5839 2 жыл бұрын
Please suggest if we want to burn from root node???????🤔
@yashwantdwala2918
@yashwantdwala2918 2 жыл бұрын
height of tree
@aishwaryshukla8880
@aishwaryshukla8880 Жыл бұрын
@@yashwantdwala2918 bro skewed tree wala test case fir bhi fail ho rha hai gfg me
@good114
@good114 Жыл бұрын
💕❤️
@26.ritukumari64
@26.ritukumari64 2 жыл бұрын
Swahaaa
@lokeshhans5036
@lokeshhans5036 2 жыл бұрын
Bhai me ak middle family se belong karta hu or mene ak laptop kharida 50000 ka 2 mahine Pehle or use ghar se koi chori karke le gya or college fess bhi nhi de abhi app meri help kr do plz 🙏🙏🙏🙏🙏
@shyamal_maity
@shyamal_maity 2 жыл бұрын
Bhaiya apne wo video kyun delete kiya "Reply to apni kaksa" ? Apka koi dosh nhi hai. Darna to usko chahiye. We are with you. Love you ❤️ kzbin.info/www/bejne/iXybdYKJdrWCebs
@dev_with_haresh
@dev_with_haresh Жыл бұрын
Too hard 😢
@i_nishantjain
@i_nishantjain 9 ай бұрын
for those who are finding why this code isn't working, let me tell you if root itself is the target then it returns 1 hence it fails just edit the code a little bit move if condition at line 36 to line 53 and replace return 1 with ans = Math.max(lh, rh); code- public static int burn(Node root,int target,Depth depth){ if(root == null)return 0; Depth ld = new Depth(-1); Depth rd = new Depth(-1); int lh = burn(root.left,target,ld); int rh = burn(root.right,target,rd); if(ld.d != -1){ ans = Math.max(ans,ld.d+1+rh); depth.d = ld.d+1; } else if(rd.d != -1){ ans = Math.max(ans,rd.d+1+lh); depth.d = rd.d+1; } if(root.data == target){ depth.d = 0; ans = Math.max(lh, rh); } return Math.max(lh,rh)+1; }
@subhankarpal2800
@subhankarpal2800 2 жыл бұрын
Sir I did this code on GFG Practice ,I understand whole logic after that I tried by my own but many times I got different result but now I wrote code as same as in your video but now still got different and , did not run any test case please help me sir , This is the Code Output --------------------------------------- 1 2 3 4 5 N 6 N N 7 8 N 9 10 11 N N N 12 N N N 13 8 Your Output: 8 Expected Output: 7 Main code ----------------------- static class Depth{ int d; public Depth(int d){ this.d = d; } } public static int minTime(Node root, int target) { // Your code goes here Depth depth = new Depth(-1); burn(root,target,depth); return ans; } static int ans = -1; public static int burn(Node root,int target,Depth depth){ if(root == null)return 0; if(root.data == target){ depth.d = 1; return 1; } Depth ld = new Depth(-1); Depth rd = new Depth(-1); int lh = burn(root.left,target,ld); int rh = burn(root.right,target,rd); if(ld.d != -1){ ans = Math.max(ans,ld.d+1+rh); depth.d = ld.d+1; } else if(rd.d != -1){ ans = Math.max(ans,rd.d+1+lh); depth.d = rd.d+1; } return Math.max(lh,rh)+1; }
@anuj2194
@anuj2194 2 жыл бұрын
same bro
@GxG_Glory
@GxG_Glory 2 жыл бұрын
Same :"(
@mrsmurf911
@mrsmurf911 2 жыл бұрын
@@GxG_Glory This approach will work only if we start to burn form the leaf node
@subhankarpal2800
@subhankarpal2800 2 жыл бұрын
Sir I did this code on GFG Practice ,I understand whole logic after that I tried by my own but many times I got different result but now I wrote code as same as in your video but now still got different and , did not run any test case please help me sir , This is the Code Output --------------------------------------- 1 2 3 4 5 N 6 N N 7 8 N 9 10 11 N N N 12 N N N 13 8 Your Output: 8 Expected Output: 7 Main code ----------------------- static class Depth{ int d; public Depth(int d){ this.d = d; } } public static int minTime(Node root, int target) { // Your code goes here Depth depth = new Depth(-1); burn(root,target,depth); return ans; } static int ans = -1; public static int burn(Node root,int target,Depth depth){ if(root == null)return 0; if(root.data == target){ depth.d = 1; return 1; } Depth ld = new Depth(-1); Depth rd = new Depth(-1); int lh = burn(root.left,target,ld); int rh = burn(root.right,target,rd); if(ld.d != -1){ ans = Math.max(ans,ld.d+1+rh); depth.d = ld.d+1; } else if(rd.d != -1){ ans = Math.max(ans,rd.d+1+lh); depth.d = rd.d+1; } return Math.max(lh,rh)+1; }
@prateekgaur2709
@prateekgaur2709 2 жыл бұрын
if(root.data == target){ depth.d = 1; //change it to 0; return 1; } 2 test case will run. But 3rd one wont.Because this code is fine only for when leaf node is burnt first.
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 38 МЛН
Gym belt !! 😂😂  @kauermotta
00:10
Tibo InShape
Рет қаралды 18 МЛН
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 32 М.
What is a Monad? - Computerphile
21:50
Computerphile
Рет қаралды 598 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 633 М.
979. Distribute Coins in Binary Tree | Tricky DFS | Recursion
17:24
Aryan Mittal
Рет қаралды 4,3 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 342 М.
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 38 МЛН