Binary Tree Maximum Path Sum (Animated Walkthrough) (LeetCode)

  Рет қаралды 23,436

AlgosWithMichael

AlgosWithMichael

Күн бұрын

Пікірлер: 137
@stephandowless1297
@stephandowless1297 3 жыл бұрын
Big up to this guy. I think he has the best explanations of leetcode problems. the animations really help if youre a visual learner
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Dude that is such a huge compliment, thank you
@darksalmon
@darksalmon Жыл бұрын
2 things of note for myself: 1) the recursive method returns only one child leg upwards because that's all that's useful to the parent. 2) the actual max contender calculation happens in only one place and it's in the form of "add me and what my left and right legs give me together". i would have thought there would be a 2nd version of this calculation in the solution (for a different shaped path), but it's folded away by clever math/logic previously. and 3) also all possible solution paths are of only one general shape = [[ left toe -> up to crotch -> down to right toe ] ] - one or both legs might be chopped off, but it's still based off that. and 4) "zero?? why zero?" - it's a shortcut - for the math it feeds to upstream it equates to "pretend this is a null child or null leg, i.e. pretend this part of the tree doesn't exist". whether the number is 0, or -2, or -200000 makes no difference....its a leg that we can abandon from all further calculations.
@mpedzi031
@mpedzi031 2 жыл бұрын
From one Michael to another, way to make our tribe proud with such good work.
@nipnip9400
@nipnip9400 3 жыл бұрын
This channel is based and redpilled and should have more subscribers. COMMENT TO BOOST THE ALGO!
@terrencecrawford3308
@terrencecrawford3308 3 жыл бұрын
SO TRUE!
@DED_Search
@DED_Search 3 жыл бұрын
Please correct me if I am wrong: 1. when you consider a subtree as the path, that is to split at current node, you sum over children's value and parent's value and update the global max 2. Otherwise, get the maximum value between left and right child, add it to the parent's value and return it to the previous level. Thanks.
@zhiqianzhang5772
@zhiqianzhang5772 2 жыл бұрын
This video is the clearest explanation on the internet!!!! Thank you so much Michael!!!
@rio512hsu
@rio512hsu 3 жыл бұрын
This is the best explanation of this problem! This one makes the other explanations look like copy pasting works without any level of understanding
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank so much!
@kamildabek_
@kamildabek_ 3 жыл бұрын
This was sick! Making those animations takes a lot of additional effort - very much appreciated dude!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you! The animations I feel is the best way to learn these problems, so it is worth it!
@jrajesh11
@jrajesh11 Ай бұрын
@@AlgosWithMichaelabsolutely 🎉
@suwin1051
@suwin1051 3 жыл бұрын
Thanks so much for the animation and code walkthrough! I finally understood the recursion concept in this problem and also why I need to add 0 to this piece of code to eliminate node value less than 0. int left = Math.max(postorder(root.left), 0); I second to what others said, excellent animation and explanation. Thanks!
@chaericherry1
@chaericherry1 2 жыл бұрын
This really is the best explanation out there for this problem. I was beating my head against the wall the entire time I was trying to solve it but this video really cleared everything up for me. Thanks for sharing!
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Of course!
@TheMadisonBluesBand
@TheMadisonBluesBand 2 күн бұрын
This is a fantastic explanation, and as others have said, the visual explanation is exquisite.
@tanvirahmadarjel6047
@tanvirahmadarjel6047 3 жыл бұрын
Your videos are quite high class and you explain a very complex problem in the possible easiest way. You deserve more followers/subscribers.
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Thank you very much!
@pawanverma9605
@pawanverma9605 2 жыл бұрын
The way u hv explained this solution hats off.. I had gone through many other explanations on KZbin but none gave me confidence that i can solve it even looking the answer.. this is the perfect representation just perfect. Commenting here just to encourage ur efforts what u r doing. Commented liked subscribed 👍🏻☺️
@pryshrng
@pryshrng 3 жыл бұрын
Thanks for putting together a great explanation; though it's not immediately clear to me why do we do left + root.val + right when calculating max. We know that the path can't be of Y shaped but we're still adding values that form a Y shape.
@abhigyanraha5620
@abhigyanraha5620 2 жыл бұрын
exactly my point!
@Roxeo1990
@Roxeo1990 2 жыл бұрын
I think its because that individual (left + root + right) might also be the max_sum_path we are looking for
@edgargranados1676
@edgargranados1676 3 жыл бұрын
Michael has the best explanation videos!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Glad you like them!
@puneethj9920
@puneethj9920 3 жыл бұрын
Best explanation on youtube with good animation thank you man!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Wow, thanks!
@khadijaashraf
@khadijaashraf 2 ай бұрын
Thanks for this nice explanation. I think there is conflict between how you are explaining the algorithm about handling the negative nodes versus how you coded. we do return negatives from our children however we ignore them afterwards.
@akbar55555shaikh
@akbar55555shaikh 3 жыл бұрын
Great, and tree traversal visualisation is awesome we return max (left, right) + root.data because we need the straight path.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you! I'm glad it was helpful
@IChowdhury01
@IChowdhury01 3 жыл бұрын
Thank you for animating this. Best explanation I found on this problem.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
No problem, glad it was helpful for you!
@terrencecrawford3308
@terrencecrawford3308 3 жыл бұрын
Michael is the CEO of being epic!
@nipnip9400
@nipnip9400 3 жыл бұрын
Accurate
@iRockTheBeat1
@iRockTheBeat1 2 жыл бұрын
I can't thank you enough for this video. Really appreciate it Michael!
@harpercfc_
@harpercfc_ 2 жыл бұрын
Thank you thank you! BTW watching you writing codes is something eye-pleasing bro is so clean and very level-minded
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Thank you, I'm glad you got like the video!
@ruthylevi9804
@ruthylevi9804 2 жыл бұрын
best explanation on this - thank you!
@ookaluke6769
@ookaluke6769 3 жыл бұрын
I like your videos! Question on 00:37 - Sum for the Path from 10 to -5 should be 10. You mentioned 0. What am i missing?
@mayurmitkari2215
@mayurmitkari2215 29 күн бұрын
This is the best explanation provided, thanks !!
@AlgosWithMichael
@AlgosWithMichael 3 күн бұрын
Thanks!
@ALEXANDER6888
@ALEXANDER6888 2 жыл бұрын
Your explanation was Godlike! Thank you very much!
@spyderwon2573
@spyderwon2573 3 жыл бұрын
Thanks for sharing this. Does replacing negatives with zero cause problems when the entire tree is negative?
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
No because we always add the node value even if it is a negative number and check if that is the new max. So if all of the tree was negative, the smallest negative number by itself would be the max.
@spyderwon2573
@spyderwon2573 3 жыл бұрын
@@AlgosWithMichael i see it now. thank you!
@nipnip9400
@nipnip9400 3 жыл бұрын
You should make a Udemy course for interview prep. I would buy in a second!
@joyceawesome1705
@joyceawesome1705 3 жыл бұрын
I agree. His explanations are like ABC. I can understand graph problems now just because of this channel
@scottloewke5199
@scottloewke5199 2 жыл бұрын
Good explanation. One suggestion: always initialize max to Integer.MIN_VALUE in the int maxPathSum(Node root) method so that successive calls can be made.
@hail_sagan2830
@hail_sagan2830 2 жыл бұрын
Just be careful to declare the variable outside of the method! If it is declared AND initialized in maxPathSum then it will not be available in the scope of the postorder method.
@isaiahparker6170
@isaiahparker6170 2 жыл бұрын
Good explanation. Question at 4:34 we substitute 0 in place of -5 but 7:01 we add -10 instead of 0. Why is that?
@ajaib1313
@ajaib1313 3 жыл бұрын
Nice animation , just by seeing it , i coded own my own and its variation too , thank you
@therollingambit5222
@therollingambit5222 2 жыл бұрын
Pls do more blind 75 qns!! Love the way u explain the approach with those animations. Looking forward to future videos :)
@xujia1001
@xujia1001 Жыл бұрын
Crystal clear. You rock it!
@GenesisWithin
@GenesisWithin 2 жыл бұрын
If our left node and right node are both negative, how do we account for which one will reduce the current node's value the least? Are we able to stop our path at that node since anything further than that would result in a negative sum?
@aakashshivanshu3578
@aakashshivanshu3578 3 жыл бұрын
That explanation was really helpful. Thanks for helping the community. Love from india ❤️
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Happy to help!
@Shellyanne5
@Shellyanne5 3 жыл бұрын
Wow I loved the way you explained that! Thank you!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Say hi to roo
@anssha2643
@anssha2643 3 жыл бұрын
Thanks Michael for such a wonderful animation. Looking forward to more such videos on trees
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
I plan to do a lot more tree problems
@juliahuanlingtong6757
@juliahuanlingtong6757 3 жыл бұрын
Without any doubt, the best explanation on this problem !!! But why poatorder travesal? I think this is the key. Could you please tell a little about the thinking process linking to the key? It would be a pefect video with 10/10. Thanks!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Postorder always will visit the three nodes bottom up and for this problem we need to be computing max's in a bottom up approach. Hope that makes sense!
@kapilrules
@kapilrules 3 жыл бұрын
Thank you for the video Michael I could not understand why we choose 0 instead of -5 but when it came to -10 we did not choose 0
@KateRipley
@KateRipley 3 жыл бұрын
another amazing video! I loved the tree animation walkthrough, it was super helpful and your explanation was concise and clear as usual :)
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you! I'm glad it was helpful :)
@GSgurusarath1
@GSgurusarath1 3 жыл бұрын
Best video for this problem
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you!
@anshumanyadav9871
@anshumanyadav9871 3 жыл бұрын
This is the best explanation video for this question. Thank you, brother. Keep making more videos like this. I liked and subscribed to you.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thanks for the sub!
@supax2
@supax2 2 жыл бұрын
You're awesome man. Keep it up!
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Appreciate it!
@anuragtiwari3032
@anuragtiwari3032 3 жыл бұрын
You deserve atleast 1million subscribers !!!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you!!!
@nomnomnam
@nomnomnam 3 жыл бұрын
Seems like you are simply mutating the global variable of max. Since you're not doing anything with the return value of postorder function, you can simply make it a void function. Great video to give the audience visuals btw. Gave you a thumbs up and a subscribe.
@esraaqandeel8465
@esraaqandeel8465 3 жыл бұрын
Thanks a lot for the animation and code walkthrough!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
No worries!
@madhuravp
@madhuravp 3 жыл бұрын
Great explanation! i was able to solve it by just looking at your algo explanation!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
That's awesome! nice job
@cavi8779
@cavi8779 3 жыл бұрын
Really appreciate your effort bro...specially that animation
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Thank you so much 😀
@QuOUseTERSEa
@QuOUseTERSEa 2 жыл бұрын
finally I understand, thank you so much!
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Glad it helped!
@chrisfair9228
@chrisfair9228 2 жыл бұрын
This may be the least on point question to this video. How, at 10:13 (ish), did you get your dual line cursor to be aligned at a different position on lines 27 and 28?
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Hold control and click :)
@staticFool
@staticFool 3 жыл бұрын
thank you so much man, for the explanation and code walk-through.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Glad it helped!
@abdelmalek9004
@abdelmalek9004 2 жыл бұрын
for the code, how it can be memorized ?
@joshithmurthy6209
@joshithmurthy6209 2 жыл бұрын
love the explaination , thanks
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Thank you
@AloneMaru
@AloneMaru 3 жыл бұрын
Great explanation and visuals!
@IbrahimBratan
@IbrahimBratan 5 ай бұрын
Thank you so much !!
@therishabhdhiman
@therishabhdhiman 2 жыл бұрын
Thanks bro. I love you.
@sanjubaloria2745
@sanjubaloria2745 3 жыл бұрын
Mind blowing animation video bro
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you so much 😀
@prashantgupta6160
@prashantgupta6160 2 жыл бұрын
thanks for awesome video
@sambasivareddyasam5712
@sambasivareddyasam5712 Ай бұрын
Thank you so much dude.
@AlgosWithMichael
@AlgosWithMichael 3 күн бұрын
No problem!
@firstjm9071
@firstjm9071 Ай бұрын
Great animations! :)
@AlgosWithMichael
@AlgosWithMichael 3 күн бұрын
Thanks! 😄
@275phuongvy
@275phuongvy 3 жыл бұрын
great explanation. Thank you
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
You are welcome!
@mylittlepurpleworld
@mylittlepurpleworld 3 жыл бұрын
thanks... really helpful
@mohitpatil1430
@mohitpatil1430 3 жыл бұрын
It was really helpful brother Thanks !
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Anytime
@dylanshine4263
@dylanshine4263 3 жыл бұрын
Great explanation!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thanks!
@kushagrakulshrestha2966
@kushagrakulshrestha2966 13 күн бұрын
at 5 : 40 you say we can't choose both, but this specific case we actually CAN choose both coz we are NOT re-visiting any node , BUT lets just say we can't still visit coz its a negative number and NOT useful when it comes down to MAX SUM PATH.... let me know if you think i'm incorrect EDIT 1: The proof of what I said can be found at 7:30 😃
@sudharsansathiamoorthy1075
@sudharsansathiamoorthy1075 3 жыл бұрын
Nice explanation!! Keep rocking Bro!!!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thank you!
@deluxebigmac
@deluxebigmac 3 жыл бұрын
Thanks, this helped a ton!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Glad it helped!
@gauravbhattacharjee4379
@gauravbhattacharjee4379 3 жыл бұрын
This is gold
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thanks!
@aniketchurihar7394
@aniketchurihar7394 2 жыл бұрын
What if the whole tree consists of negative numbers only, then in your case wouldn't it be returning zero instead of the maximum of all the negative numbers?
@MrX-zf5gl
@MrX-zf5gl 3 ай бұрын
That's why we initialise max is -inf and not zero
@LeHoangTu
@LeHoangTu 3 жыл бұрын
Nicely done!
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thanks!
@eddanapudir
@eddanapudir 3 жыл бұрын
Thanks for the video. Still not clear why it should be rounded to 0.
@eddanapudir
@eddanapudir 3 жыл бұрын
I meant at 5:12
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
We round to zero because if we return a negative number, the max sum can only get smaller.
@vinprabhu
@vinprabhu 3 жыл бұрын
Good explanation as always
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
I appreciate that!
@kaleemullahnizamani7436
@kaleemullahnizamani7436 3 жыл бұрын
10:40 why is this Math.max used? i can't understand the reason
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Because we are trying to figure out the max path. If we didn't use the max function you would only be calculating local maximums
@vabiohandoko5679
@vabiohandoko5679 2 жыл бұрын
I just don't understand how it started from the bottom of the tree based on the code? Can anybody explain it to me?
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
Sorry this comment is late, but the reason why is because the recursive function will continuously move left until it hits the base case of the node being null. After that it moves back up the parent and goes right, then explores the left subtree again.
@juan2thepaab
@juan2thepaab 3 жыл бұрын
how the f are we supposed to figure this out in an interview?? I can see coming up with the brute force approach, but the returning 0 stuff is very specific. Is this a common pattern that we would also see in other problems?
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
If you do enough recursive BT problems, you will get experience solving them with subtle nuances. This question definitely is harder than most though
@heisenberg1844
@heisenberg1844 3 жыл бұрын
Amazed.
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Haha thank you
@codingessential91
@codingessential91 3 жыл бұрын
man i feel bad u dont have more subs. not for u, but if u had more subs I'm sure u wud have worked harder and churned out more such videos. anyways, great service
@AlgosWithMichael
@AlgosWithMichael 2 жыл бұрын
I appreciate it! I don't do it for the subs, but more subs would be great haha
@Garensonic
@Garensonic 3 жыл бұрын
I got this question in my Amazon onsite and I got totally flustered. I have my Google phone screen next week and I am very scared
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Best of luck to you!
@raahim88991
@raahim88991 3 жыл бұрын
perfectt
@AlgosWithMichael
@AlgosWithMichael 3 жыл бұрын
Thanks!
Binary Tree Maximum Path Sum - DFS - Leetcode 124 - Python
15:19
Longest Increasing Path in a Matrix (DFS + Memoization)
18:47
AlgosWithMichael
Рет қаралды 19 М.
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,5 МЛН
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 166 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
Minimum Window Substring | Sliding Window | LeetCode
18:00
AlgosWithMichael
Рет қаралды 40 М.
LeetCode 124. Binary Tree Maximum Path Sum (Algorithm Explained)
16:16
L17. Maximum Path Sum in Binary Tree | C++ | Java
17:50
take U forward
Рет қаралды 362 М.
BINARY TREE MAX PATH SUM | PYTHON | LEETCODE 124
20:16
Cracking FAANG
Рет қаралды 1,4 М.
Linked List in Binary Tree (BFS + DFS + Preorder Traversal)
17:43
AlgosWithMichael
Рет қаралды 8 М.
Kadane's Algorithm - Maximum Subarray (Dynamic Programming)
8:24
AlgosWithMichael
Рет қаралды 26 М.
Binary tree maximum path sum | Leetcode #124
15:23
Techdose
Рет қаралды 55 М.
Binary Tree Max Path Sum (LeetCode Day 29)
9:33
Errichto Algorithms
Рет қаралды 28 М.
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 2,5 МЛН