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

Поделиться
HTML-код
  • Опубликовано: 11 фев 2025

Комментарии • 139

  • @stephandowless1297
    @stephandowless1297 4 года назад +11

    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  4 года назад +1

      Dude that is such a huge compliment, thank you

  • @kamildabek_
    @kamildabek_ 3 года назад +3

    This was sick! Making those animations takes a lot of additional effort - very much appreciated dude!

    • @AlgosWithMichael
      @AlgosWithMichael  3 года назад +3

      Thank you! The animations I feel is the best way to learn these problems, so it is worth it!

    • @jrajesh11
      @jrajesh11 4 месяца назад

      @@AlgosWithMichaelabsolutely 🎉

  • @mpedzi031
    @mpedzi031 2 года назад

    From one Michael to another, way to make our tribe proud with such good work.

  • @darksalmon
    @darksalmon Год назад +1

    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.

  • @suwin1051
    @suwin1051 3 года назад +2

    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!

  • @naraStanford
    @naraStanford 2 года назад

    This video is the clearest explanation on the internet!!!! Thank you so much Michael!!!

  • @tanvirahmadarjel6047
    @tanvirahmadarjel6047 3 года назад +3

    Your videos are quite high class and you explain a very complex problem in the possible easiest way. You deserve more followers/subscribers.

  • @pawanverma9605
    @pawanverma9605 2 года назад

    The way u hv explained this solution hats off.. I had gone through many other explanations on RUclips 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 👍🏻☺️

  • @iRockTheBeat1
    @iRockTheBeat1 2 года назад +1

    I can't thank you enough for this video. Really appreciate it Michael!

  • @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!

  • @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

  • @puneethj9920
    @puneethj9920 3 года назад +2

    Best explanation on youtube with good animation thank you man!

  • @DED_Search
    @DED_Search 3 года назад +1

    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.

  • @IChowdhury01
    @IChowdhury01 3 года назад +1

    Thank you for animating this. Best explanation I found on this problem.

  • @nipnip9400
    @nipnip9400 3 года назад +3

    This channel is based and redpilled and should have more subscribers. COMMENT TO BOOST THE ALGO!

  • @KateRipley
    @KateRipley 4 года назад +1

    another amazing video! I loved the tree animation walkthrough, it was super helpful and your explanation was concise and clear as usual :)

  • @TheMadisonBluesBand
    @TheMadisonBluesBand 2 месяца назад

    This is a fantastic explanation, and as others have said, the visual explanation is exquisite.

  • @harpercfc_
    @harpercfc_ 2 года назад

    Thank you thank you! BTW watching you writing codes is something eye-pleasing bro is so clean and very level-minded

  • @anssha2643
    @anssha2643 4 года назад +1

    Thanks Michael for such a wonderful animation. Looking forward to more such videos on trees

  • @edgargranados1676
    @edgargranados1676 4 года назад

    Michael has the best explanation videos!

  • @anshumanyadav9871
    @anshumanyadav9871 4 года назад +1

    This is the best explanation video for this question. Thank you, brother. Keep making more videos like this. I liked and subscribed to you.

  • @therollingambit5222
    @therollingambit5222 2 года назад +1

    Pls do more blind 75 qns!! Love the way u explain the approach with those animations. Looking forward to future videos :)

  • @ruthylevi9804
    @ruthylevi9804 3 года назад +1

    best explanation on this - thank you!

  • @Shellyanne5
    @Shellyanne5 4 года назад +2

    Wow I loved the way you explained that! Thank you!

  • @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?

  • @ajaib1313
    @ajaib1313 3 года назад +1

    Nice animation , just by seeing it , i coded own my own and its variation too , thank you

  • @mayurmitkari2215
    @mayurmitkari2215 3 месяца назад

    This is the best explanation provided, thanks !!

  • @pryshrng
    @pryshrng 3 года назад +8

    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 3 года назад

      exactly my point!

    • @Roxeo1990
      @Roxeo1990 2 года назад +1

      I think its because that individual (left + root + right) might also be the max_sum_path we are looking for

  • @ookaluke6769
    @ookaluke6769 3 года назад +1

    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?

  • @aakashshivanshu3578
    @aakashshivanshu3578 3 года назад

    That explanation was really helpful. Thanks for helping the community. Love from india ❤️

  • @ALEXANDER6888
    @ALEXANDER6888 2 года назад

    Your explanation was Godlike! Thank you very much!

  • @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?

  • @xujia1001
    @xujia1001 Год назад

    Crystal clear. You rock it!

  • @khadijaashraf
    @khadijaashraf 5 месяцев назад

    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.

  • @kushagrakulshrestha2966
    @kushagrakulshrestha2966 3 месяца назад

    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 😃

  • @supax2
    @supax2 3 года назад

    You're awesome man. Keep it up!

  • @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.

  • @scottloewke5199
    @scottloewke5199 3 года назад

    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.

  • @terrencecrawford3308
    @terrencecrawford3308 3 года назад +2

    Michael is the CEO of being epic!

  • @nipnip9400
    @nipnip9400 3 года назад +5

    You should make a Udemy course for interview prep. I would buy in a second!

    • @nneka-mama-chari
      @nneka-mama-chari 3 года назад

      I agree. His explanations are like ABC. I can understand graph problems now just because of this channel

  • @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?

  • @esraaqandeel8465
    @esraaqandeel8465 3 года назад

    Thanks a lot for the animation and code walkthrough!

  • @abdelmalek9004
    @abdelmalek9004 2 года назад

    for the code, how it can be memorized ?

  • @staticFool
    @staticFool 3 года назад

    thank you so much man, for the explanation and code walk-through.

  • @anuragtiwari3032
    @anuragtiwari3032 3 года назад

    You deserve atleast 1million subscribers !!!

  • @madhuravp
    @madhuravp 4 года назад

    Great explanation! i was able to solve it by just looking at your algo explanation!

  • @spyderwon2573
    @spyderwon2573 3 года назад +1

    Thanks for sharing this. Does replacing negatives with zero cause problems when the entire tree is negative?

    • @AlgosWithMichael
      @AlgosWithMichael  3 года назад +1

      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!

  • @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

  • @QuOUseTERSEa
    @QuOUseTERSEa 2 года назад

    finally I understand, thank you so much!

  • @cavi8779
    @cavi8779 3 года назад

    Really appreciate your effort bro...specially that animation

  • @dp2120
    @dp2120 28 дней назад

    I don't understand. On line 29, you assign a variable "max" but never use it.

  • @AloneMaru
    @AloneMaru 3 года назад

    Great explanation and visuals!

  • @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!

  • @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  3 года назад

      I appreciate it! I don't do it for the subs, but more subs would be great haha

  • @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

  • @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 6 месяцев назад

      That's why we initialise max is -inf and not zero

  • @sudharsansathiamoorthy1075
    @sudharsansathiamoorthy1075 4 года назад

    Nice explanation!! Keep rocking Bro!!!

  • @juan2thepaab
    @juan2thepaab 4 года назад +1

    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 года назад +1

      If you do enough recursive BT problems, you will get experience solving them with subtle nuances. This question definitely is harder than most though

  • @mohitpatil1430
    @mohitpatil1430 4 года назад

    It was really helpful brother
    Thanks !

  • @firstjm9071
    @firstjm9071 3 месяца назад

    Great animations! :)

  • @IbrahimBratan
    @IbrahimBratan 7 месяцев назад

    Thank you so much !!

  • @joshithmurthy6209
    @joshithmurthy6209 2 года назад

    love the explaination , thanks

  • @GSgurusarath1
    @GSgurusarath1 3 года назад

    Best video for this problem

  • @deluxebigmac
    @deluxebigmac 3 года назад

    Thanks, this helped a ton!

  • @therishabhdhiman
    @therishabhdhiman 2 года назад

    Thanks bro. I love you.

  • @sambasivareddyasam5712
    @sambasivareddyasam5712 4 месяца назад

    Thank you so much dude.

  • @prashantgupta6160
    @prashantgupta6160 2 года назад

    thanks for awesome video

  • @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.

  • @sanjubaloria2745
    @sanjubaloria2745 4 года назад

    Mind blowing animation video bro

  • @mylittlepurpleworld
    @mylittlepurpleworld 3 года назад

    thanks... really helpful

  • @dylanshine4263
    @dylanshine4263 3 года назад

    Great explanation!

  • @275phuongvy
    @275phuongvy 4 года назад

    great explanation. Thank you

  • @LeHoangTu
    @LeHoangTu 4 года назад

    Nicely done!

  • @vinprabhu
    @vinprabhu 4 года назад

    Good explanation as always

  • @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

  • @eddanapudir
    @eddanapudir 4 года назад

    Thanks for the video. Still not clear why it should be rounded to 0.

    • @eddanapudir
      @eddanapudir 4 года назад

      I meant at 5:12

    • @AlgosWithMichael
      @AlgosWithMichael  4 года назад

      We round to zero because if we return a negative number, the max sum can only get smaller.

  • @gauravbhattacharjee4379
    @gauravbhattacharjee4379 3 года назад

    This is gold

  • @heisenberg1844
    @heisenberg1844 4 года назад

    Amazed.

  • @raahim88991
    @raahim88991 3 года назад

    perfectt