Binary Tree - 81: Get Max Sum between two Leaves in Binary Tree
ฝัง
- เผยแพร่เมื่อ 19 ก.ย. 2024
- Source Code:thecodingsimpl...
Solution:
- We start binary tree traversal in post order manner
- We'll take a variable maxSum = 0
- Now while traversing in post order, for each node, we'll store left + right + node value
- If at any moment, if this value is greater than maxSum value then update the maxSum value
Time Complexity: O(n)
Space Complexity: O(1)
Do Watch video for more info
CHECK OUT CODING SIMPLIFIED
/ codingsimplified
★☆★ VIEW THE BLOG POST: ★☆★
thecodingsimpli...
I started my TH-cam channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 200+ videos.
★☆★ SUBSCRIBE TO ME ON TH-cam: ★☆★
www.youtube.co...
★☆★ Send us mail at: ★☆★
Email: thecodingsimplified@gmail.com
Your channel is gold!!❤❤, I will share it as much as possible. Lots of love!!
Thanks for it. Keep Watching.
very good explaination bhai
Thanks for nice feedback.
Best explanation for this question. Thank you so much 😀.
Thanks for your nice feedback. Keep Watching.
Exactly gfg soln was killing my head man
Truly loved the way of explaining
Thanks for your nice feedback. Keep Watching.
Best and complete explanation!! Well Done sir.
Just one correction : maxSum should be INT_MIN instead of 0.
That's right. We should take this INT_MIN / Integer.Min_Value instead of 0. Thanks for notifying.
I wonder why the views are so less. This video is very well explained!
Thanks for your nice feedback. Keep Watching.
Very Nicely explained Sir !! Plzz make more videos on graph and Tree. Thanks for your effort in making this video.
Thanks. Sure.
@@CodingSimplified good. Explain
nicely explained..understandable!!!
Glad it helped you. Thanks.
Sir when i am run this code in gfg it produce wrong output for some cases , can u please explain why it is ??
www.geeksforgeeks.org/find-maximum-path-sum-two-leaves-binary-tree/
There is a case of skewed tree which we need to handle separately. Even i was stuck on this testcase. Rest my logic was exactly same as this video said.
Thanks for answering.
@@nikhilpatro5905 So, What should we do if it it is asked in coding tests?
@@JohnWick-kh7ow Check out the gfg article i have linked before. If the result is INT_MIN then return the returned value of the function. That will be the sum of all nodes from root to leaf in trees where all nodes have only 1 child.
@@nikhilpatro5905 ok, I have understood. But I don't think I will be able to think of that case in tests.
nice
Sir your function is returning max(l,r)+1 everytime while ans of the problem is stored in maxSum which is never returned. For example discussed in video , when finally we reach to root node , we have l=10 and r = 12,so maxSUm = 10+12+2 = 24 which is correct but your functions return max(10,12)+2 = 14.
Pure gold!! :)
Thanks for your nice feedback. Keep Watching.
just return INT_MIN....rather than going to further test cases
Hi, what if there is only one single node : lets say [1], your variable maxSumBetweenTwoleaves will still be 0 but the answer should be 1
Thanks Nishant for notifying this. That's right. For this, at the starting of function, we can check, if there's only single node in tree, return this node value.
@@CodingSimplified Thanks for this video! Actually @Nishant there is surprisingly no issue with having a single node. The algo shown here never updated maxSum when the current node has no children. So, the algo would correctly output maxSum = DEFAULT VALUE (either INT_MIN or NONE).
Also, by the way, if there _were_ an issue with a single node, there would also be an issue with having the "tree" be one long branch, it would still only have one node with neither a non-null left pointer nor non-null right pointer.
sir this code does not work for 5 N 6 -5 5 the answer should be 16 but it returns 6