Binary Tree Maximum Path Sum - (Google, Meta, Amazon, Flipkart) : Explanation ➕ Live Coding
Вставка
- Опубліковано 10 лют 2025
- This is the 18th Video of our Binary Tree Playlist.
In this video we will try to solve a very good Qn "Binary Tree Maximum Path Sum".
We will do live coding after explanation and see if we are able to pass all the test cases.
Problem Name : Binary Tree Maximum Path Sum
Company Tags : Google, Meta, Amazon, Flipkart
My solutions on Github : github.com/MAZ...
Leetcode Link : leetcode.com/p...
GfG Link : practice.geeks...
My GitHub Repo for interview preparation : github.com/MAZ...
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
This is the first video that I have watched and I have to admit I'm in love with your voice as well as your teaching skills. Thank you is not enough for the work you are doing. I am glad I found you :) Soon will let you know what I achieved after watching all your videos :) God bless you Comrade
Thank you so much
Means a lot 😊
MIK bhai to poora famous hogae hain 😍
This guy has some exceptional skills of explaining code . Mtlb itna ache s kon smjhata h yar. Har ek question ese smjhaya jata h jese hum chote bache he aur mil bhaiya hmare bade bhaiya.
He always keeps in mind ki koi bhi doubt na rahe hamare mind me and isliye br br vo hr cheej repeat bhi krte rhte he.
This guy can really give a tough competition to Striver in terms of Smooth Explanation, easy to understand way of teaching and the simplicity with which he makes us understand. Legend in making!
Thank you so much Chinmoy for your appreciation ❤️❤️❤️
@@codestorywithMIK Could you please upload a video solution of leetcode 2467. Most profitable path in a Tree?
Totally agree
True
True
I have no words, Just keep it up, youtube ka best education channel bn skta hai ye.
Thanks a lot Mayank❤️❤️❤️
Already ban gaya hai ab to lagta hai
I understand that the variables were funny but that's where magic happens 😆
Time Complexity : O(n) -> Because we are visiting every node only once.
Sach me, variables are funny, magic variables.
Thanks a lot 😇
great explanation man
I was also stuck at what to return
So glad to know Aditya ❤️❤️❤️
You are a magician bro
bhaiiya i was watching your video in a browser where I didn't had any logged in account. But after watching i felt like "Yr iss bande ko subscribe aur like nhi krenge ko kisko krenge"....
Thanks Pure dil se for this great explanation. Now onwards I will ask doubts also from you whenever I am stuck in a problem.
Thank you so so much Tauquir
You made my day ❤️❤️❤️
Sahi baat
I thought no one could beat striver in dsa, but then I saw this channel.
At this point, I am now convinced that this guy is the best in DSA among any other UA-cam channels
Bro!!!!!! Salute to your Explanation 👏🙌
thanks a lot ..mai iske pehle 2 3 video dekhi kuch nhi samjha appka ek baaar mai samajh gya..
When a man is hungry, it is better to teach him to fish than to give him a fish » attributed to Confucius . « If you give a man a fish, he’ll eat someday, if you teach him to fish, he’ll always eat
that's what you are doing.........rather than just giving the solution....you are
teaching us how to build logic🔥🙌
Indeed. So apt . I totally agree. This guy is something else
Thank you so much.
Your comment made my day ❤️❤️❤️❤️
@@codestorywithMIK your explanations make my day🙌..............after watching your video i feel like i have learnt something
@@oqant0424 Means a lot ❣
itna khatarnak koi kaise explain kar sakta hai mujhe to code he yaad ho gaya is question ka sach me ... itna mast explanation !!! hard to very easy banadiya aap ne ❤
Bhaiya, bahut bahut shukriya! 🙌 Main kuch dino se aapke videos follow kar raha hoon, lekin aaj aapne realize karwaya ki fundamentals samajhna kitna zaroori hai. 🔥 Aapne is video me bataya ki agar left + right + root->data return karenge to path ke definition ko hi violate kar denge! Matlab, pattern samajhne se pehle fundamentals clear karna bahut zaroori hai. ❤
I nominate this questions's explanation as one of your top10. This channel feels like being friends with topper. 👻
Means a lot 😇
you are nothing but a magician. No one can beat this level of explanation as of now on youtube.
Indeed the variables were 🤣
But dude, you nailed it. You are literally crazy.
I have never ever seen this kind of thorough explanation. More power to you bro
💪
Glad to know 😂😂😂
And thanks a lot ❤️❤️❤️
what a guy!! this knowledge cannot be achieved even in paid courses! you are the king
Bhai u r not the next anyone u r our first MIK. Mark my words this channel is going to become the best beating everyone else. Top class explanation.
This means a lot ❤️❤️🙏🙏
Please start the DSA from zero advance if possible and make it huge , you will loved by everyone for sure
Sure Manish. I have a plan for that as well. Couldn’t start due to time crunch.
But planning it soon .
Thanks again for your valuable feedback ❤️❤️❤️
Really great bhaiya you are really legend in explanation of code literally anybody who see your way of explanation will become truly fan of yours
your Hindi handwriting is also great bhaiya and you are absolutely right by your variable naming when I think of those variable names whole code came into my mind once again thanks bhaiya 😊😊😇
Thank you so much Uday ❤️❤️❤️
You are a gem bhai even though i have solved the question but came here to watch your solution cause I always learn something from your video.
Thank you for this amazing explanation.
Thank you so much Danish ❤️❤️❤️
great explanation bhaiya!
Noted Pranav. Thank you so much for watching ❣
reach++
bhai aag laga di.
Waise toh mane ye qn karliya tha but supporting your channel bro🔥
Thank you so much Piyush ❤️❤️❤️
@@codestorywithMIK bhai yeh variable naming bohot accha hai yar while practicing.
fokat ka comment likhna nahi padta har variable ke liye.
Bro you rock😎😎
Thanks a lot ❤️❤️❤️
I think c++ code is more easy to understand in terms of what to return
int maxPathSumUtil(Node* root, int& res)
{
if (root == nullptr)
return 0;
// Recursive calls for the left and right subtrees
int ls = maxPathSumUtil(root->left, res);
int rs = maxPathSumUtil(root->right, res);
// Max path sum at the current node
int temp = max(max(ls, rs) + root->data, root->data);
// Max path sum considering both children
int maxVal = max(temp, ls + rs + root->data);
// Update the overall maximum path sum
res = max(res, maxVal);
return temp; //return sum for current node
}
int maxPathSum(Node* root) {
int res = INT_MIN;
maxPathSumUtil(root, res);
return res;
}
Your channel is going to grow soon man
Thanks a lot Manish.
It means a lot to me. I just want all of us to grow together and not fall for paid courses in market. We can do it together 💪💪💪
Thanks again ❤️❤️❤️
best explanation sir , earlier i was just mugging up this question, but now i found the right approach ,Thank You!
The variables really did the magic 😄
You are a LEGEND
Soon people will know about you ❤
what an explanation!!!!!!!!!!
Thank you so so much ❤️❤️❤️
Bhai kya hi explanation hai yaar..😮 5 min me code krke uda diya mzaa aagya logic sunke..❤🎉
Thank you so much Tushar 😇🙏
@@codestorywithMIK Thanx to you vai🫡
you are genius ,the level of detail you have provided in this solution is amazing
Means a lot. Thank you so much 😇🙏❤️
OMG, this is so good reasoning, You have my heart, so easy pezzy to crack such questions where similar approach is used.
Thanks a lot Gomzy ❤️
Really appreciate your comment 💝💝💝
great explaination bhaiya after watching intuition I was able to write code on my own. Thanks a lotttt
Thank you 😇🙏❤️
I was stuck in the proper explanation of line by line code, previously I knew the concept of finding max path sum but now I understood the code properly
Thank you so much ❤️❤️
Striver se bas smjh aaya tha bhai ne feel de diya ❤❤
Mom look i found gold for motivating me to keep doing leetcode!
please suggest me any resources for learning stack queue linked list basics only for now ,, ASAP
Thanks a lot Chinmay.
I would suggest you to study any topic like this :
1) Understand the DS (example : stack)
2) Solve popular qns on them (Easy and Medium) . Don’t solve random ones. Solve which are frequently asked in interviews.
3) For company and interview qns : github.com/MAZHARMIK/Interview_DS_Algo
@@codestorywithMIK already starred your repo, but I need to learn stack , linked list, queue from basics , for that I need resources, can you suggest some
I learned them separately sometimes from UA-cam, sometimes from GfG.
I think, for leading a topic, GfG is good enough.
Then for solving qns, go for leetcode
@@codestorywithMIK ok got it..
expalined and dri run from the example was nice
Bhai bade bade UA-camrs k video dekhe. Lekin isse acha explanation kahi nahi mila. Guruji ho aap.
If anyone can beat this level of explanation, I am ready to loose a bet of 1000 😂😂😂
Kuch din bad inka channel har jagah dikhega.
Inke jaisi explanation aur intuition koi nhi de pata .
Dude u are just great.....🥺❤️ though the length of your videos are long..but each minute is worth watching 🙌🏻🥺
I appreciate the love! Glad you find it worthwhile. 😇🙏
thanks bhaiya last part hi dekhne ayaa tha aur apne wahi bataya hai
u cn beat the legend, i don't mind spending time with your videos if they are helpful good job
great explanation bhaiya
Thanks a lot Dilesh ❤️❤️❤️
Maujj dila di bhai
Thank you so much Harshit ❤️❤️
bhaiya phli barr achhe se smj m aaya abhi tk code ratt rkha tha, ab #no_ratta_maar🥰
Nice Explanation dude
I Pray magic will happen with your this journey on youtube.
Thanks a lot ❤️❤️❤️
kya bat bhai matlb maza agya...keep it up man🔥🔥
❤️❤️ thank you 😊
nice approach...love from Bangladesh❤
Thanks for being consistent.
Bro you will soon have a blast of subscribers....you are too good🔥
It means a lot to me ❤️❤️❤️
as usual you are amazing ,unmatchable ,unbeatable in domain of explaining the concepts and intution
Amazing Explainations Sir!!!
Love your explanation!
Really Bhaiya, You are Great, You made this hard level question very easy ... you explained all the points very clearly. Thanku so much Bhaiya for providing us such a great videos which are helping us a lot in our Preparation. Really loved the your way of teaching and the explanation.
It means a lot 🙏😇
Great Video bhaiya!
Thanks a lot! Took me a good time to understand , but I was sure you can simply it.
this channel will never disappoint you.
Thank you so much . Means a lot to me 😇🙏
Thanku so much for THE GREAT EXPLANATION
Tooo Good 🙌
Yrr bhaiya maza aagya ❤️
Dude loved this video man I am never gonna forget this answer
🙏🙏❤️❤️
Explanation was so easyy, i was like ki hogya bss itta hi, im loving ur videos nd learning alot from you, thankyou so much❤️
Thanks a lot Aman 💕
wow , beautifully explained .
I was stuck on this ques for such a long time after seeing so many youtube videos but you , sir explained this concept so beautifully that I'm in awe.
Also sir please make a q&a video , would love to know your journey ❤
It means a lot. Thank you so much ❤️❤️🙏🙏
Sure, I will make a video soon.
Feel free to share with your friends and colleagues ❤️❤️
beautifully explained
Thank you! 🙂
Bhai bhot hi mst explanation, contest k solution bhi kra dia kro please
Thank you so much.❤️❤️
Soon plans
you will be next striver for us .. may be even better than him .. keep it up
such a beautiful explanation
JUST GREAT🤩
Thank you so much 😇🙏
Dhanyawad guruji
Means a lot. Thank you so much 😇🙏
one comment for your god level teaching ❤
❤️🙏
beautifully explained!
Thank you so much Divyansh ❤️❤️
Great explanation ! Just subscribed this channel....
Thanks a lot Dhruv ❤️❤️❤️
Great explanation 🫡
Only video on this problem which is actually helpful.
Lots of love to you bro
Took the code of finding diameter and made some changes.
It got accepted 😅
Mujhe myCodeSchool waale teacher ki yaad agai .
I wonder, ya to aap wahi waale Sir ho, ya unke bhai ya bete honge 🤔
Nation wants to know 🤣
😂😂😂😂
MyCodeSchool’s tutor is a legend .
I stand nowhere compared to him 😁
But to answer your qn,
I am neither his son or brother 😂😂😂
Thanks btw ❤️
I’ll take that as a compliment 😅
This is brilliant...
Thank you so much 😇🙏❤️
Python Buds :)
# It's same as Diameter of Binary Tree
class Solution:
def maxPathSum(self, root: Optional[TreeNode]) -> int:
if not root :
return 0
res = float('-inf')
def helper(root) :
nonlocal res
if not root :
return 0
left = max(helper(root.left),0) if root.left else 0
right = max(helper(root.right),0) if root.right else 0
res = max(res,left + right + root.val)
return max(left,right) + root.val
helper(root)
return res
just the additional case, to handle negative paths, koi branching agar negative sums return kr rhi ho ,us case mei 0 return kardo ,
AMAZING EXPLANATION, UNBEATABLE
Thank you for watching 😇🙏
AMAZING
Means a lot ❤️😇🙏
Op sir
Thank you 😇🙏❤️
same bhaiya .....intuition aagya tha,code likh diya tha ......bas return kya karna hai wahi khel samapt ho gya.......
❤
Class solution {
Static int max Down sum(TreeNode root,int[]max value){
if(root==null)return0;
intleft sum=MATH.MAX
(0,max Down Sum((root.left,max value));
int right Sum=MATH.MAX (0 max Down sum(root. right ,max value));
max value (0)= Math.Max (max Value (0),left sum+right sum+root. Value);
return root. Value +Math.Max (left sum,right. Sum);
}
Public []int max pathsum (Treenode root){
int[]max value =new int[1];
Max value [0]=integer.MIN_VALUE;
max down sum(root,max value); return max value (0);
now,only i understood this...kon acha hai and kyu?,.. :)
🤯🤯🤯❤❤
❤️❤️❤️❤️
please explain why we are adding l ,it is pointer why its not l.val and r.val when adding to root.val
bhaiya kya m ak youtube channel open kar lu vha par m bhi solution smja du ga , koi nhi smj aya tho ak baar appka sol dhek lu ga kabhi kabhi match bhi so saktha hai sol
agr root vale value he negative hogi to hum kon sai case ko consider karengai
maxSum ko humne initially maxSum = INT_MIN kar rakha hai.
So, root’s negative value will be greater than INT_MIN ,
So in that case we only take that root’s value which is the only choice we are left with.
The best way how i handle these, is to take a small example of the scenario you want to understand and do a dry run.
It always helps me
this is q-1373 of leetcode?
Hi, {name}
kaunsa editor use kia hai or tablet connected hai?
It’s the default Notes app of ipad 11 pro
where is the case when adding root->val is decreasing the sum lets say root->val is -1000 l is 20 r is 30
Jisne apka Diameter of Binary Tree vala video dekha hoga vo esse easily bna lega...
I literally copy pasted that code and did some changes.
It got accepted 😅.
After seeing his solution I lost respect for this problem 😅
🥹💓👌
subscribe kr diya bhai....
video thumbnail & channel logo thoda improve ho skta h. Pdhate to bdhiya ho hi
Sure.
Point taken. Will improve them soon.
Thanks a lot ❤️❤️❤️
My solution: leetcode.com/problems/binary-tree-maximum-path-sum/solutions/6213461/beats-100-users-approach-recursive-dfs-e-0ee4