Big up to this guy. I think he has the best explanations of leetcode problems. the animations really help if youre a visual learner
@AlgosWithMichael3 жыл бұрын
Dude that is such a huge compliment, thank you
@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.
@mpedzi0312 жыл бұрын
From one Michael to another, way to make our tribe proud with such good work.
@kamildabek_3 жыл бұрын
This was sick! Making those animations takes a lot of additional effort - very much appreciated dude!
@AlgosWithMichael3 жыл бұрын
Thank you! The animations I feel is the best way to learn these problems, so it is worth it!
@jrajesh112 ай бұрын
@@AlgosWithMichaelabsolutely 🎉
@DED_Search3 жыл бұрын
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.
@suwin10513 жыл бұрын
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!
@zhiqianzhang57722 жыл бұрын
This video is the clearest explanation on the internet!!!! Thank you so much Michael!!!
@tanvirahmadarjel60473 жыл бұрын
Your videos are quite high class and you explain a very complex problem in the possible easiest way. You deserve more followers/subscribers.
@AlgosWithMichael2 жыл бұрын
Thank you very much!
@nipnip94003 жыл бұрын
This channel is based and redpilled and should have more subscribers. COMMENT TO BOOST THE ALGO!
@terrencecrawford33083 жыл бұрын
SO TRUE!
@chaericherry12 жыл бұрын
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!
@AlgosWithMichael2 жыл бұрын
Of course!
@rio512hsu3 жыл бұрын
This is the best explanation of this problem! This one makes the other explanations look like copy pasting works without any level of understanding
@AlgosWithMichael3 жыл бұрын
Thank so much!
@TheMadisonBluesBand6 күн бұрын
This is a fantastic explanation, and as others have said, the visual explanation is exquisite.
@iRockTheBeat12 жыл бұрын
I can't thank you enough for this video. Really appreciate it Michael!
@pryshrng3 жыл бұрын
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.
@abhigyanraha56202 жыл бұрын
exactly my point!
@Roxeo19902 жыл бұрын
I think its because that individual (left + root + right) might also be the max_sum_path we are looking for
@pawanverma96052 жыл бұрын
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 👍🏻☺️
@puneethj99203 жыл бұрын
Best explanation on youtube with good animation thank you man!
@AlgosWithMichael3 жыл бұрын
Wow, thanks!
@IChowdhury013 жыл бұрын
Thank you for animating this. Best explanation I found on this problem.
@AlgosWithMichael3 жыл бұрын
No problem, glad it was helpful for you!
@KateRipley3 жыл бұрын
another amazing video! I loved the tree animation walkthrough, it was super helpful and your explanation was concise and clear as usual :)
@AlgosWithMichael3 жыл бұрын
Thank you! I'm glad it was helpful :)
@edgargranados16763 жыл бұрын
Michael has the best explanation videos!
@AlgosWithMichael3 жыл бұрын
Glad you like them!
@anssha26433 жыл бұрын
Thanks Michael for such a wonderful animation. Looking forward to more such videos on trees
@AlgosWithMichael3 жыл бұрын
I plan to do a lot more tree problems
@ruthylevi98043 жыл бұрын
best explanation on this - thank you!
@terrencecrawford33083 жыл бұрын
Michael is the CEO of being epic!
@nipnip94003 жыл бұрын
Accurate
@therollingambit52222 жыл бұрын
Pls do more blind 75 qns!! Love the way u explain the approach with those animations. Looking forward to future videos :)
@mayurmitkari2215Ай бұрын
This is the best explanation provided, thanks !!
@AlgosWithMichael7 күн бұрын
Thanks!
@ajaib13133 жыл бұрын
Nice animation , just by seeing it , i coded own my own and its variation too , thank you
@harpercfc_2 жыл бұрын
Thank you thank you! BTW watching you writing codes is something eye-pleasing bro is so clean and very level-minded
@AlgosWithMichael2 жыл бұрын
Thank you, I'm glad you got like the video!
@xujia1001 Жыл бұрын
Crystal clear. You rock it!
@anshumanyadav98713 жыл бұрын
This is the best explanation video for this question. Thank you, brother. Keep making more videos like this. I liked and subscribed to you.
@AlgosWithMichael3 жыл бұрын
Thanks for the sub!
@Shellyanne53 жыл бұрын
Wow I loved the way you explained that! Thank you!
@AlgosWithMichael3 жыл бұрын
Say hi to roo
@supax22 жыл бұрын
You're awesome man. Keep it up!
@AlgosWithMichael2 жыл бұрын
Appreciate it!
@ALEXANDER68882 жыл бұрын
Your explanation was Godlike! Thank you very much!
@nipnip94003 жыл бұрын
You should make a Udemy course for interview prep. I would buy in a second!
@joyceawesome17053 жыл бұрын
I agree. His explanations are like ABC. I can understand graph problems now just because of this channel
@akbar55555shaikh3 жыл бұрын
Great, and tree traversal visualisation is awesome we return max (left, right) + root.data because we need the straight path.
@AlgosWithMichael3 жыл бұрын
Thank you! I'm glad it was helpful
@ookaluke67693 жыл бұрын
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?
@khadijaashraf2 ай бұрын
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.
@aakashshivanshu35783 жыл бұрын
That explanation was really helpful. Thanks for helping the community. Love from india ❤️
@AlgosWithMichael3 жыл бұрын
Happy to help!
@spyderwon25733 жыл бұрын
Thanks for sharing this. Does replacing negatives with zero cause problems when the entire tree is negative?
@AlgosWithMichael3 жыл бұрын
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.
@spyderwon25733 жыл бұрын
@@AlgosWithMichael i see it now. thank you!
@GenesisWithin2 жыл бұрын
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?
@scottloewke51992 жыл бұрын
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_sagan28302 жыл бұрын
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.
@anuragtiwari30323 жыл бұрын
You deserve atleast 1million subscribers !!!
@AlgosWithMichael3 жыл бұрын
Thank you!!!
@nomnomnam3 жыл бұрын
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.
@isaiahparker61702 жыл бұрын
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?
@esraaqandeel84653 жыл бұрын
Thanks a lot for the animation and code walkthrough!
@AlgosWithMichael3 жыл бұрын
No worries!
@staticFool3 жыл бұрын
thank you so much man, for the explanation and code walk-through.
@AlgosWithMichael3 жыл бұрын
Glad it helped!
@codingessential913 жыл бұрын
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
@AlgosWithMichael2 жыл бұрын
I appreciate it! I don't do it for the subs, but more subs would be great haha
@AloneMaru3 жыл бұрын
Great explanation and visuals!
@chrisfair92282 жыл бұрын
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?
@AlgosWithMichael2 жыл бұрын
Hold control and click :)
@madhuravp3 жыл бұрын
Great explanation! i was able to solve it by just looking at your algo explanation!
@AlgosWithMichael3 жыл бұрын
That's awesome! nice job
@juliahuanlingtong67573 жыл бұрын
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!
@AlgosWithMichael3 жыл бұрын
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!
@QuOUseTERSEa2 жыл бұрын
finally I understand, thank you so much!
@AlgosWithMichael2 жыл бұрын
Glad it helped!
@kapilrules3 жыл бұрын
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
@kushagrakulshrestha296617 күн бұрын
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 😃
@GSgurusarath13 жыл бұрын
Best video for this problem
@AlgosWithMichael3 жыл бұрын
Thank you!
@cavi87793 жыл бұрын
Really appreciate your effort bro...specially that animation
@AlgosWithMichael2 жыл бұрын
Thank you so much 😀
@IbrahimBratan5 ай бұрын
Thank you so much !!
@firstjm9071Ай бұрын
Great animations! :)
@AlgosWithMichael7 күн бұрын
Thanks! 😄
@therishabhdhiman2 жыл бұрын
Thanks bro. I love you.
@joshithmurthy62092 жыл бұрын
love the explaination , thanks
@AlgosWithMichael2 жыл бұрын
Thank you
@juan2thepaab3 жыл бұрын
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?
@AlgosWithMichael3 жыл бұрын
If you do enough recursive BT problems, you will get experience solving them with subtle nuances. This question definitely is harder than most though
@prashantgupta61602 жыл бұрын
thanks for awesome video
@sambasivareddyasam5712Ай бұрын
Thank you so much dude.
@AlgosWithMichael7 күн бұрын
No problem!
@sudharsansathiamoorthy10753 жыл бұрын
Nice explanation!! Keep rocking Bro!!!
@AlgosWithMichael3 жыл бұрын
Thank you!
@mohitpatil14303 жыл бұрын
It was really helpful brother Thanks !
@AlgosWithMichael3 жыл бұрын
Anytime
@deluxebigmac3 жыл бұрын
Thanks, this helped a ton!
@AlgosWithMichael3 жыл бұрын
Glad it helped!
@mylittlepurpleworld3 жыл бұрын
thanks... really helpful
@sanjubaloria27453 жыл бұрын
Mind blowing animation video bro
@AlgosWithMichael3 жыл бұрын
Thank you so much 😀
@aniketchurihar73942 жыл бұрын
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-zf5gl3 ай бұрын
That's why we initialise max is -inf and not zero
@275phuongvy3 жыл бұрын
great explanation. Thank you
@AlgosWithMichael3 жыл бұрын
You are welcome!
@dylanshine42633 жыл бұрын
Great explanation!
@AlgosWithMichael3 жыл бұрын
Thanks!
@abdelmalek90042 жыл бұрын
for the code, how it can be memorized ?
@vinprabhu3 жыл бұрын
Good explanation as always
@AlgosWithMichael3 жыл бұрын
I appreciate that!
@LeHoangTu3 жыл бұрын
Nicely done!
@AlgosWithMichael3 жыл бұрын
Thanks!
@vabiohandoko56792 жыл бұрын
I just don't understand how it started from the bottom of the tree based on the code? Can anybody explain it to me?
@AlgosWithMichael2 жыл бұрын
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.
@gauravbhattacharjee43793 жыл бұрын
This is gold
@AlgosWithMichael3 жыл бұрын
Thanks!
@kaleemullahnizamani74363 жыл бұрын
10:40 why is this Math.max used? i can't understand the reason
@AlgosWithMichael3 жыл бұрын
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
@eddanapudir3 жыл бұрын
Thanks for the video. Still not clear why it should be rounded to 0.
@eddanapudir3 жыл бұрын
I meant at 5:12
@AlgosWithMichael3 жыл бұрын
We round to zero because if we return a negative number, the max sum can only get smaller.
@Garensonic3 жыл бұрын
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