Binary tree maximum path sum | Leetcode

This video explains a very important interview programming question which is to find the maximum path sum in a binary tree. This is a very important binary tree question and the problem is very similar to finding diameter of a binary tree. I have explained the intuition for solving this problem including all the cases to be handled. I have explained the code flow with proper examples and code explanation is present at the end of the video.As usual, CODE LINK is present in the description below. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
CODE LINK: gist.github.com/SuryaPratapK/...
Similar Problem:
Diameter of a binary tree: • Diameter of a binary t...

Пікірлер: 206

  • @uwontlikeit
    @uwontlikeit3 жыл бұрын

    1:09 - EXAMPLE 2 IS INCORRECT. max sum = 6 (not 5) and includes the root, and the rightest node

  • @shubhiagarwal4047
    @shubhiagarwal40472 жыл бұрын

    Sir , this explanation deserves much more applaud , that's why I shared your channel to all my juniors.

  • @yimingzhao3753
    @yimingzhao37534 жыл бұрын

    Seen a few explanations on KZread, this is the best one so far, way to go!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    :)

  • @ayaramzy6815

    @ayaramzy6815

    2 жыл бұрын

    @@techdose4u yea the best one

  • @jagdishwarbiradar1763
    @jagdishwarbiradar17634 жыл бұрын

    Well explained with example 🍁 , Smart way by using the three cases it clear all my doubts 👍🏻.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks man :)

  • @ssss-jd9cl

    @ssss-jd9cl

    2 жыл бұрын

    can anyone explain why m21 has taken value max of ms and left+right+root->data. we can take only some value of left+right+node also. I am getting confused in this. Anyone explain.

  • @saharshrathi7828
    @saharshrathi78284 жыл бұрын

    Such a great explanation! I just watched the video to understand the algorithm and you explained it so good! Then coded the algorithm by myself and the solution got accepted in the first try! Subscribed.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @m.praneethreddy7335
    @m.praneethreddy73354 жыл бұрын

    Best channel in the youtube for data structures and algorithms👌👌

  • @julietgeorge4858
    @julietgeorge48584 жыл бұрын

    Thank you for showing/explaining the algorithm before the code. I use javascript and so it is helpful.

  • @lemax2980
    @lemax29804 жыл бұрын

    Respect for the effort! 🙃. Thank you for the great content and please , if you are comfortable,teach us about graphs after the leetcode saga😅

  • @dumbcurious450
    @dumbcurious4503 жыл бұрын

    Probably the best explanation of this problem on You Tube .. Thanks Man

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @sangramkapre
    @sangramkapre4 жыл бұрын

    Nice! The only explanation I understood on KZread :-)

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    :)

  • @punittiwari7557
    @punittiwari75573 жыл бұрын

    The example which you have illustrated where the sum was 5, the maximum sum we can achieve is 3->2->(-1) ->2 sum=6

  • @sankarikarthick801
    @sankarikarthick8014 жыл бұрын

    This is the best explanation I have come across for this question.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @tarunsingh3687
    @tarunsingh36873 жыл бұрын

    Damn!! It was leetcode HARD I understood at one go. Kudos to you ! You provide quality content. Keep making videos and thank you so much.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @hiteshgupta7652
    @hiteshgupta76524 жыл бұрын

    Amazing Explanation Sir, Looking forward to more hard questions like these!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    They will come in may challenge :)

  • @adityadhikle9473
    @adityadhikle94732 жыл бұрын

    Amazing explanation!!! Thank you

  • @aryamarda1643
    @aryamarda16433 жыл бұрын

    I was able to do it by your bt diameter ques. Explanation. Thank you 😀

  • @LaxmiNarayana-zs5vz
    @LaxmiNarayana-zs5vz3 жыл бұрын

    Thanks for the great explanation!. how to actually store the path and print the nodes involved in our maximum path?

  • @babumon5351
    @babumon53513 жыл бұрын

    Really good explanation. I started to understand the logic when with a playback speed of .75. Thanks

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Nice :)

  • @mikasaackerman2694
    @mikasaackerman26944 жыл бұрын

    Nice explanation!! The solution felt really intuitive after looking at this video

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @jlecampana
    @jlecampana3 жыл бұрын

    Great Video, best explanation available. To me the tricky part was to understand why m21 needs to be a max(ms, left + right + root->val) and not just: left + right + root->val. Could you maybe elaborate on that. Thank you!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks. Because there might be a max sum path in either left or right subtree which is not rooted at current node.

  • @sayyadalikhan3702
    @sayyadalikhan37024 жыл бұрын

    Excellent explanation! Best Video for Binary tree maximum path sum

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @raviagarwal4287
    @raviagarwal42873 жыл бұрын

    The way in which you explained this complex problem is lit💥 Keep it up👍

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @aneeshsharma
    @aneeshsharma3 жыл бұрын

    Very good explanation of the Cases. Thank You !

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome bro :)

  • @utkarshgupta6258
    @utkarshgupta62582 жыл бұрын

    By far the best explanation

  • @mrshubh101
    @mrshubh1012 жыл бұрын

    Excellent explanation! I understood this in the first go. I'm now curious to know, since every recursive approach also has an iterative approach, how will this problem be solved iteratively, without recursion? Also, since we've kept track of the max sum, can we also extend this problem to return a list of nodes which are part of max sum path? Like in the above example, max sum = 18, nodes = [8->4->1->5]

  • @deepanshu6576
    @deepanshu65764 жыл бұрын

    worlds best video ,sir aur bnayo aisi interview bit se lekr ,clear all the doubts

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @koyavasudhalakshmi2073
    @koyavasudhalakshmi20733 жыл бұрын

    Sir ,could u please provide the link for inserting nodes into binary tree dynamically at what ever place we want, But in generally I found inserting via level order in binary tree.

  • @anujasrivastava3446
    @anujasrivastava34462 жыл бұрын

    This is a really nice explanation! Thank you!

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome :)

  • @MyLifeMyWorld08
    @MyLifeMyWorld082 жыл бұрын

    One of the best explanation Surya. Thanks

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome

  • @sarahzaman3173
    @sarahzaman31732 жыл бұрын

    thank you, this was the best explanation I could find.

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome 😊

  • @Neerajkumar-xl9kx
    @Neerajkumar-xl9kx3 жыл бұрын

    can't be explained better than this...thank you

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @mohitshoww
    @mohitshoww4 жыл бұрын

    No one Can Explain Like You .

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    😁 Thanks

  • @dhruv2014
    @dhruv20142 жыл бұрын

    Great explanation

  • @harshagarwal2629
    @harshagarwal26293 жыл бұрын

    Supppppppperrrrbbbbb explanation....U just made a hard problem look so easier . Explanation is awesome :)

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @competitivedoritos4294
    @competitivedoritos42944 жыл бұрын

    i searched tons of resource but couldn't understand this problem, thank you so much sir for this video :)

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @viralmehta2542
    @viralmehta25423 жыл бұрын

    very nicely explained in detailed.

  • @harishkumarreddy079
    @harishkumarreddy0793 жыл бұрын

    very good explanation for a hard problem

  • @mohamedmusaraf675
    @mohamedmusaraf6754 жыл бұрын

    Thanks a lot.!!

  • @aashishkalia8269
    @aashishkalia82694 жыл бұрын

    You explain really nice and simple !!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @DMDRPBHU
    @DMDRPBHU3 жыл бұрын

    he saves results as max which would o/p the max sum but doest return the result since the left or right side of the tress must be a straight iine and not a sub tree

  • @dimplerohara3799
    @dimplerohara37993 жыл бұрын

    Best explanation so far!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @salonikaur7043
    @salonikaur70433 жыл бұрын

    Why do we return case 1 in the helper function?

  • @varunnadipudi2257
    @varunnadipudi22573 жыл бұрын

    Well done!!Explanation is very nice.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @biswaMastAadmi
    @biswaMastAadmi2 жыл бұрын

    excellent explanation ! keep going !

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Thanks ☺️

  • @sakibmalik1613
    @sakibmalik16134 жыл бұрын

    So beautiful, I done it the hard way it got AC anyway, I was unsure of what to return to the parent LOL

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    😅

  • @shubhambhandari431
    @shubhambhandari4314 жыл бұрын

    how much time and practice needs to solve problems like this? n how to do?

  • @divyamgupta2100
    @divyamgupta21003 жыл бұрын

    Can you explain why only case 1 is returned, only that part is confusing me.

  • @ShivamSingh-zj4be
    @ShivamSingh-zj4be3 жыл бұрын

    sir, why we return ms after three case??

  • @vaibhavjain4710
    @vaibhavjain47103 жыл бұрын

    Best explanation , I have ever seen ;)

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @chetanpatteparapu7600
    @chetanpatteparapu76004 жыл бұрын

    Thank you very much Mate. Very nice explanation

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @usrivastava92
    @usrivastava924 жыл бұрын

    brilliant explanation.. thanks alot.. :)

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome :)

  • @recklessekta
    @recklessekta3 жыл бұрын

    very well explained, thanks!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @programmingstuff5741
    @programmingstuff57414 жыл бұрын

    These are also such questions to prepare for. Interviews

  • @samkitjain2136
    @samkitjain21362 жыл бұрын

    bhai aap legend ho yaar

  • @saumya1singh
    @saumya1singh4 жыл бұрын

    Very Well Explained!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @yashgoswami5374
    @yashgoswami53744 жыл бұрын

    good explanation sir

  • @x12624
    @x126242 жыл бұрын

    Why are we returning the case1 value at the end instead of the result?

  • @mansibhardwaj3976
    @mansibhardwaj39764 жыл бұрын

    Excellently explained!!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @deviprajwala9779
    @deviprajwala97793 жыл бұрын

    Amazing explanation!!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @arnavkarforma3015
    @arnavkarforma30154 жыл бұрын

    I liked your solution on diameter of a trees, and it seemed to me jus same question with one difference that here there were negative values. Just because I was familiar with that approach used that class Solution { public int maxPathSum(TreeNode root) { return maxPathSumRec(root)[1]; } int[] maxPathSumRec(TreeNode root){ int largestSum = -99999; if(root == null){ return new int[]{0,largestSum}; } int[] left = maxPathSumRec(root.left); int[] right = maxPathSumRec(root.right); left[0] = Math.max(left[0], 0); right[0] = Math.max(right[0], 0); largestSum = Math.max((left[0] + right[0] + root.val), Math.max(left[1],right[1])); return new int[]{Math.max(left[0],right[0])+root.val,largestSum}; } }

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    👍

  • @factsclub4u
    @factsclub4u3 күн бұрын

    @Techdose just one doubt i had why we are returning case 1 value only and hence returning max_straight .Please give clear explaination.

  • @markos8955
    @markos89554 жыл бұрын

    Nice Explanation........Nice to see having no Dislikes.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @InderjeetSingh-no3pp
    @InderjeetSingh-no3pp3 жыл бұрын

    Nice explanation.....easily understandable

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @bhairavas2528
    @bhairavas25283 жыл бұрын

    Can you please mention TC and SC for this approach??

  • @chhaviagarwal7514
    @chhaviagarwal75144 жыл бұрын

    very well explained.. Thanks

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome

  • @MagicalCreationAviCreation
    @MagicalCreationAviCreation4 жыл бұрын

    Sir in 112. Path Sum(Leetcode problem) Sir consider one case when my sum is lesser than the zero (after reaches of some node). and however we know my solution is not in this sub tree, i am simply continuing recursion tiil i reaches leaf node. sir is there any solution to this ....... [5,4,8,11,null,13,4,7,2,null,null,null,1,3,4] sum == 22 here after i reaches the node 7 , here i get know this is not the best path,however i am continuing recursion of leaf node 3 and 4 sir if i break the recursion at node 7 we can reduce time complexity am i right sir......

  • @gautamgoel2024
    @gautamgoel20243 жыл бұрын

    This is a hard problem to understand but sir you made it easy❤️

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    👍🏼

  • @gautamgoel2024

    @gautamgoel2024

    3 жыл бұрын

    @@techdose4u sir can we have a video on🙏 print all ''k"" sum paths in a Binary tree🙏

  • @PriyankaSharma02499
    @PriyankaSharma024993 жыл бұрын

    Very nice explanation

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks

  • @yuhaokong
    @yuhaokong4 жыл бұрын

    ok this explanation is pretty damn clear

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @tanmayagupta4591
    @tanmayagupta45912 жыл бұрын

    Thanks!

  • @techdose4u

    @techdose4u

    2 жыл бұрын

    Welcome :)

  • @chaunguyen8202
    @chaunguyen82023 жыл бұрын

    Thank you!

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @madhavsahi7400
    @madhavsahi74004 жыл бұрын

    at 1.46 in the second tree example why u havent consider -1 , 2 in ur path ?? the maxsum will be 6 which is greater than 5 as in ur selected path

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Yes you are correct. Actually I wanted it to be -2 but it turned out to be 2. My bad. Consider that -2.

  • @madhavsahi7400

    @madhavsahi7400

    4 жыл бұрын

    @@techdose4u okay sir thanks

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks for pointing out :)

  • @madhavsahi7400

    @madhavsahi7400

    4 жыл бұрын

    @@techdose4u one doubt sir...I also solve problems in C++ ...and I wanted to ask what those code statements at line 29,30 mean ?...

  • @PhoenixRisingFromAshes471
    @PhoenixRisingFromAshes4714 жыл бұрын

    outstanding solution brother

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks bro :)

  • @aniketsriwastva6345
    @aniketsriwastva63454 жыл бұрын

    Great Explaination Sir

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks

  • @spk9434
    @spk94344 жыл бұрын

    Excellent explanation.

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @humansofcrypto9746
    @humansofcrypto97464 жыл бұрын

    Excellent!

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Thanks :)

  • @aadhavan-arunkumar
    @aadhavan-arunkumar3 жыл бұрын

    Amazing explanation!. Do you mind sharing where do you work?

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Currently I am teaching students and working professionals and guiding them to get better opportunities :) Follow me on LinkedIn to know more

  • @ayushthakur733
    @ayushthakur7333 жыл бұрын

    Amazing explanation 👌

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks 😀

  • @AnkitKumar-mb4vl
    @AnkitKumar-mb4vl3 жыл бұрын

    Great explanation ❤️

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @yashikagarg4044
    @yashikagarg40443 жыл бұрын

    Please make video on path sum question where we have to check if path with given sum exists on leet code

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Maybe I will take this when I do TREE.

  • @rocknroll7967
    @rocknroll79673 жыл бұрын

    How to come up with this solution in interview if we have not seen this problem before?

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    You must have seen this problem. This is one the most common tree problem. There can be multiple variations of this. If you have solved only 1 of them then you can get the idea.

  • @rocknroll7967

    @rocknroll7967

    3 жыл бұрын

    @@techdose4u I learnt this the hard way. Thanks for the awesome video

  • @harshagarwal2629
    @harshagarwal26293 жыл бұрын

    Hey, please make a video on sum of distances in a tree question from leetcode .

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Will try

  • @nickscriv5141
    @nickscriv51413 жыл бұрын

    Why is it traversing to every node 3 times? It looks like the traversal just visits each node once.

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Going to child and then coming back and again going to another child. It's technically 1 visit but recursion makes it 3 times if you consider going from child to parent as a visite :)

  • @priyanshukhullar5836
    @priyanshukhullar58363 жыл бұрын

    great explanation

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Thanks :)

  • @saiteja8870
    @saiteja8870 Жыл бұрын

    🔥🔥🔥🔥

  • @kamalkantrajput7684
    @kamalkantrajput76843 жыл бұрын

    thank u so much Bhaiya

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome :)

  • @RahulKumar-wv4ti
    @RahulKumar-wv4ti3 жыл бұрын

    Thanks bro

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @pawanchandra9193
    @pawanchandra91933 жыл бұрын

    Dope

  • @afnaanrafique366
    @afnaanrafique366 Жыл бұрын

    java implementation not passing all test cases on leetcode

  • @sanky52
    @sanky523 жыл бұрын

    for input -9 6 -10 output is 6 but expected output is -13 in gfg

  • @ketanahuja8939
    @ketanahuja89393 жыл бұрын

    Wonderful

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    :)

  • @willturner3440
    @willturner34403 жыл бұрын

    Thanks bhai

  • @techdose4u

    @techdose4u

    3 жыл бұрын

    Welcome

  • @shivamsingh7219
    @shivamsingh72193 жыл бұрын

    Why you returning max_straight in recursion function.

  • @shivamsingh7219

    @shivamsingh7219

    3 жыл бұрын

    I got it bcz we want left and right value of curr node that's why returning max_straight

  • @indsonusharma
    @indsonusharma4 жыл бұрын

    Great man I did this same too bus mera live video me reply de diya karo haha

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Live video mein itne comments aate hain ke sambhal hi nhi paata 😅 random answer deta hun....don't mind.

  • @midhileshmomidi3120
    @midhileshmomidi31202 жыл бұрын

    Why are we returning max-straight? why we can't we return result. I didn't get this

  • @cipherCrafters14
    @cipherCrafters144 жыл бұрын

    Sir please help me i m math teacher in school i want to make online video on youtube please tell me which digital board you use sir please sir

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Ping me on LinkedIn.

  • @dhawaldhingra
    @dhawaldhingra2 жыл бұрын

    In 2nd example stating at 1:00 minute, the maximum path of the tree should be 6 and not 5. The max path still goes through the root. It goes like- 3->2->-1->2 :)

  • @ashutoshshukla5253
    @ashutoshshukla52534 жыл бұрын

    why we returning max_s instead result???

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    I have explained this clearly. We are returning MS (case-1) value only because result is keeping track of max path sum found so far. It may or may not include the current node. Also, result is passed by reference and hence latest changes are available at places. No need to return result. The only thing we want to return is MS because we want to check if we can find some other path which will have more path sum as compared to current maximum stored in result.

  • @ashutoshshukla5253

    @ashutoshshukla5253

    4 жыл бұрын

    got the solution keep uploading more videos it help our coding skills

  • @subham-raj
    @subham-raj4 жыл бұрын

    *This is so sexy*

  • @techdose4u

    @techdose4u

    4 жыл бұрын

    Welcome bro :)

  • @huansir1922
    @huansir19222 жыл бұрын

    buddy , your view is special