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

  Рет қаралды 35,398

Anuj Bhaiya

Anuj Bhaiya

Күн бұрын

Пікірлер: 71
@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😊
@laveshgarg2189
@laveshgarg2189 2 жыл бұрын
Thanku Bhaiya for the amazing consistency in dsa one course
@RiteshSingh-rz7tz
@RiteshSingh-rz7tz 2 жыл бұрын
Thanks for continuing this series. I am addicted to it.
@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?
@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 🙏🏻🛑⭕🛑⭕🛑⭕🛑⭕
@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
@abhinavvarshney6366
@abhinavvarshney6366 2 жыл бұрын
.... THANK YOU ANUJ BHAIYA....
@abacas2175
@abacas2175 2 жыл бұрын
First comment... Bhayiya java complete course daliya please..i like ur way of teaching!!!!
@annanyavijay4267
@annanyavijay4267 2 жыл бұрын
Can we use static depth variable instead of making it a wrapper class?
@sagarchawla4926
@sagarchawla4926 2 жыл бұрын
Did you get the answer?
@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.....
@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 5 ай бұрын
I have modified it to make it right.
@akshaypatil8940
@akshaypatil8940 5 ай бұрын
If (target == root.data) { Int height = Math.max (burnTree(root.right), burnTree(root.left)); ans = Math.max(ans, height); return 1; }
@rajeevshetty8929
@rajeevshetty8929 4 ай бұрын
@@akshaypatil8940 return ans
@ashwiniskumar4275
@ashwiniskumar4275 2 жыл бұрын
Will this work only for burn from leaf node or burn from any target node.
@yashyadav7927
@yashyadav7927 2 жыл бұрын
same question ... bhaiya if u there plz reply us
@vasuaggarwal-p2k
@vasuaggarwal-p2k 7 ай бұрын
Can we use greedy by taking the node which in the centre of the most depth path
@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
@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 2 жыл бұрын
Yes
@laveshgarg2189
@laveshgarg2189 2 жыл бұрын
Bhaiya the approach was mind blowing maza aa gaya Thanku Bhaiya
@reenayadav8468
@reenayadav8468 2 жыл бұрын
Thank you sir
@nittinrana2277
@nittinrana2277 2 жыл бұрын
Bahiya thanks for maintaining consistency
@keshavkamalkarn1431
@keshavkamalkarn1431 2 жыл бұрын
Thankyou bhai for your guidance and Bhaiya Is data structure and algorithm different for different languages?
@warriorgaming9509
@warriorgaming9509 2 жыл бұрын
NO
@Sanyamjain77
@Sanyamjain77 2 жыл бұрын
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
@saritaprasad4295
@saritaprasad4295 Жыл бұрын
Thanks
@Lifeofkashh31
@Lifeofkashh31 2 жыл бұрын
agya samjh rhanks a lot
@aryanvarma2536
@aryanvarma2536 2 жыл бұрын
Hello bhaiya
@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
@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
@sdwivedi
@sdwivedi 2 жыл бұрын
Thanks bhaiya
@pkyadav6230
@pkyadav6230 Жыл бұрын
7:24 that's ampersand ' &'
@sudhanshudubey7267
@sudhanshudubey7267 2 жыл бұрын
bhaiya please make video on hackathons
@chinmaychaudhari4143
@chinmaychaudhari4143 2 жыл бұрын
Bhaiya topic wise dsa ki ek question sheet banva do na ... That would help us lot to practice while learning dsa...
@sarangavadiya7087
@sarangavadiya7087 2 жыл бұрын
thank you bhaiya.
@laplinanayak7688
@laplinanayak7688 2 жыл бұрын
how we can install jdk in chrombook please upload vedio on this topic
@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; }
@bharatDarshaByDrone
@bharatDarshaByDrone 2 жыл бұрын
Felling First With 6 others
@pranavmittal8249
@pranavmittal8249 2 жыл бұрын
Bhaiya ek video git commands ke uper bhi bna 😣
@ankushaviraj8292
@ankushaviraj8292 2 жыл бұрын
Bhaiya Java ka full course apni kasha pe show nahi hora hai kaha se dekhe
@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
@priyanshumohanty5261
@priyanshumohanty5261 2 жыл бұрын
1:16 Poora ka poora tree swaha 😂
@26.ritukumari64
@26.ritukumari64 2 жыл бұрын
Swahaaa
@mrrishiraj88
@mrrishiraj88 2 жыл бұрын
🙏👍👍
@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 🙏🙏🙏🙏🙏
@arpanmaheshwari2290
@arpanmaheshwari2290 2 жыл бұрын
First
@good114
@good114 Жыл бұрын
💕❤️
@i_nishantjain
@i_nishantjain 11 ай бұрын
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; }
@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 😢
@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
@hamzaansari5714
@hamzaansari5714 2 жыл бұрын
Thank you bhai
@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.
Lecture 67: Minimum Time to BURN the Entire Binary Tree || C++ Placement Series
28:28
Fake watermelon by Secret Vlog
00:16
Secret Vlog
Рет қаралды 26 МЛН
1 сквиш тебе или 2 другому? 😌 #шортс #виола
00:36
Сюрприз для Златы на день рождения
00:10
Victoria Portfolio
Рет қаралды 2,4 МЛН
All nodes distance K in binary tree | Leetcode #863
12:45
Techdose
Рет қаралды 9 М.
Amazon Software Engineer Interview: Print Left View of Binary Tree
40:34
L21. Vertical Order Traversal of Binary Tree | C++ | Java
18:53
take U forward
Рет қаралды 325 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 664 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 622 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 421 М.
Morris Inorder Tree Traversal
11:44
Tushar Roy - Coding Made Simple
Рет қаралды 146 М.
Fake watermelon by Secret Vlog
00:16
Secret Vlog
Рет қаралды 26 МЛН