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
1:09 - EXAMPLE 2 IS INCORRECT. max sum = 6 (not 5) and includes the root, and the rightest node
Sir , this explanation deserves much more applaud , that's why I shared your channel to all my juniors.
Seen a few explanations on KZread, this is the best one so far, way to go!
@techdose4u
4 жыл бұрын
:)
@ayaramzy6815
2 жыл бұрын
@@techdose4u yea the best one
Well explained with example 🍁 , Smart way by using the three cases it clear all my doubts 👍🏻.
@techdose4u
4 жыл бұрын
Thanks man :)
@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.
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
4 жыл бұрын
Thanks :)
Best channel in the youtube for data structures and algorithms👌👌
Thank you for showing/explaining the algorithm before the code. I use javascript and so it is helpful.
Respect for the effort! 🙃. Thank you for the great content and please , if you are comfortable,teach us about graphs after the leetcode saga😅
Probably the best explanation of this problem on You Tube .. Thanks Man
@techdose4u
3 жыл бұрын
Welcome :)
Nice! The only explanation I understood on KZread :-)
@techdose4u
4 жыл бұрын
:)
The example which you have illustrated where the sum was 5, the maximum sum we can achieve is 3->2->(-1) ->2 sum=6
This is the best explanation I have come across for this question.
@techdose4u
4 жыл бұрын
Thanks :)
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
3 жыл бұрын
Welcome :)
Amazing Explanation Sir, Looking forward to more hard questions like these!
@techdose4u
4 жыл бұрын
They will come in may challenge :)
Amazing explanation!!! Thank you
I was able to do it by your bt diameter ques. Explanation. Thank you 😀
Thanks for the great explanation!. how to actually store the path and print the nodes involved in our maximum path?
Really good explanation. I started to understand the logic when with a playback speed of .75. Thanks
@techdose4u
3 жыл бұрын
Nice :)
Nice explanation!! The solution felt really intuitive after looking at this video
@techdose4u
4 жыл бұрын
Thanks
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
3 жыл бұрын
Thanks. Because there might be a max sum path in either left or right subtree which is not rooted at current node.
Excellent explanation! Best Video for Binary tree maximum path sum
@techdose4u
4 жыл бұрын
Thanks :)
The way in which you explained this complex problem is lit💥 Keep it up👍
@techdose4u
3 жыл бұрын
Thanks :)
Very good explanation of the Cases. Thank You !
@techdose4u
3 жыл бұрын
Welcome bro :)
By far the best explanation
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]
worlds best video ,sir aur bnayo aisi interview bit se lekr ,clear all the doubts
@techdose4u
4 жыл бұрын
Thanks
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.
This is a really nice explanation! Thank you!
@techdose4u
2 жыл бұрын
Welcome :)
One of the best explanation Surya. Thanks
@techdose4u
2 жыл бұрын
Welcome
thank you, this was the best explanation I could find.
@techdose4u
2 жыл бұрын
Welcome 😊
can't be explained better than this...thank you
@techdose4u
3 жыл бұрын
Welcome :)
No one Can Explain Like You .
@techdose4u
4 жыл бұрын
😁 Thanks
Great explanation
Supppppppperrrrbbbbb explanation....U just made a hard problem look so easier . Explanation is awesome :)
@techdose4u
3 жыл бұрын
Thanks
i searched tons of resource but couldn't understand this problem, thank you so much sir for this video :)
@techdose4u
4 жыл бұрын
Welcome :)
very nicely explained in detailed.
very good explanation for a hard problem
Thanks a lot.!!
You explain really nice and simple !!
@techdose4u
4 жыл бұрын
Thanks :)
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
Best explanation so far!
@techdose4u
3 жыл бұрын
Thanks :)
Why do we return case 1 in the helper function?
Well done!!Explanation is very nice.
@techdose4u
3 жыл бұрын
Thanks
excellent explanation ! keep going !
@techdose4u
2 жыл бұрын
Thanks ☺️
So beautiful, I done it the hard way it got AC anyway, I was unsure of what to return to the parent LOL
@techdose4u
4 жыл бұрын
😅
how much time and practice needs to solve problems like this? n how to do?
Can you explain why only case 1 is returned, only that part is confusing me.
sir, why we return ms after three case??
Best explanation , I have ever seen ;)
@techdose4u
3 жыл бұрын
Thanks :)
Thank you very much Mate. Very nice explanation
@techdose4u
4 жыл бұрын
Welcome :)
brilliant explanation.. thanks alot.. :)
@techdose4u
4 жыл бұрын
Welcome :)
very well explained, thanks!
@techdose4u
3 жыл бұрын
Welcome :)
These are also such questions to prepare for. Interviews
bhai aap legend ho yaar
Very Well Explained!
@techdose4u
4 жыл бұрын
Thanks
good explanation sir
Why are we returning the case1 value at the end instead of the result?
Excellently explained!!
@techdose4u
4 жыл бұрын
Thanks
Amazing explanation!!
@techdose4u
3 жыл бұрын
Thanks
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
4 жыл бұрын
👍
@Techdose just one doubt i had why we are returning case 1 value only and hence returning max_straight .Please give clear explaination.
Nice Explanation........Nice to see having no Dislikes.
@techdose4u
4 жыл бұрын
Thanks :)
Nice explanation.....easily understandable
@techdose4u
3 жыл бұрын
Thanks :)
Can you please mention TC and SC for this approach??
very well explained.. Thanks
@techdose4u
4 жыл бұрын
Welcome
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......
This is a hard problem to understand but sir you made it easy❤️
@techdose4u
3 жыл бұрын
👍🏼
@gautamgoel2024
3 жыл бұрын
@@techdose4u sir can we have a video on🙏 print all ''k"" sum paths in a Binary tree🙏
Very nice explanation
@techdose4u
3 жыл бұрын
Thanks
ok this explanation is pretty damn clear
@techdose4u
4 жыл бұрын
Thanks :)
Thanks!
@techdose4u
2 жыл бұрын
Welcome :)
Thank you!
@techdose4u
3 жыл бұрын
Welcome
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
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
4 жыл бұрын
@@techdose4u okay sir thanks
@techdose4u
4 жыл бұрын
Thanks for pointing out :)
@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 ?...
outstanding solution brother
@techdose4u
4 жыл бұрын
Thanks bro :)
Great Explaination Sir
@techdose4u
4 жыл бұрын
Thanks
Excellent explanation.
@techdose4u
4 жыл бұрын
Thanks :)
Excellent!
@techdose4u
4 жыл бұрын
Thanks :)
Amazing explanation!. Do you mind sharing where do you work?
@techdose4u
3 жыл бұрын
Currently I am teaching students and working professionals and guiding them to get better opportunities :) Follow me on LinkedIn to know more
Amazing explanation 👌
@techdose4u
3 жыл бұрын
Thanks 😀
Great explanation ❤️
@techdose4u
3 жыл бұрын
Thanks :)
Please make video on path sum question where we have to check if path with given sum exists on leet code
@techdose4u
3 жыл бұрын
Maybe I will take this when I do TREE.
How to come up with this solution in interview if we have not seen this problem before?
@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
3 жыл бұрын
@@techdose4u I learnt this the hard way. Thanks for the awesome video
Hey, please make a video on sum of distances in a tree question from leetcode .
@techdose4u
3 жыл бұрын
Will try
Why is it traversing to every node 3 times? It looks like the traversal just visits each node once.
@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 :)
great explanation
@techdose4u
3 жыл бұрын
Thanks :)
🔥🔥🔥🔥
thank u so much Bhaiya
@techdose4u
3 жыл бұрын
Welcome :)
Thanks bro
@techdose4u
3 жыл бұрын
Welcome
Dope
java implementation not passing all test cases on leetcode
for input -9 6 -10 output is 6 but expected output is -13 in gfg
Wonderful
@techdose4u
3 жыл бұрын
:)
Thanks bhai
@techdose4u
3 жыл бұрын
Welcome
Why you returning max_straight in recursion function.
@shivamsingh7219
3 жыл бұрын
I got it bcz we want left and right value of curr node that's why returning max_straight
Great man I did this same too bus mera live video me reply de diya karo haha
@techdose4u
4 жыл бұрын
Live video mein itne comments aate hain ke sambhal hi nhi paata 😅 random answer deta hun....don't mind.
Why are we returning max-straight? why we can't we return result. I didn't get this
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
4 жыл бұрын
Ping me on LinkedIn.
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 :)
why we returning max_s instead result???
@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
4 жыл бұрын
got the solution keep uploading more videos it help our coding skills
*This is so sexy*
@techdose4u
4 жыл бұрын
Welcome bro :)
buddy , your view is special