LeetCode 124. Binary Tree Maximum Path Sum (Algorithm Explained)

  Рет қаралды 37,729

Nick White

Nick White

Күн бұрын

Пікірлер: 50
@haoshenyu
@haoshenyu 4 жыл бұрын
//this is Post Order Traversal, goes from bottom up to the top //for max_path_sum, we add all positive values of left subtree,right subtree and root and find the maximum //for the result of method pathSum, we return only the larger sum path from bottom up, either the left or the right, because from a subroot you can only choose one direction to sum
@evgeni-nabokov
@evgeni-nabokov 4 жыл бұрын
Why don't you explain why you get a max(0, left/right)? You are just repeating after what you are typing. Is this called "algorithm explained"? Same question to the return value.
@leomonz
@leomonz 4 жыл бұрын
I think 0 just avoid getting negative number... he didn't explain it.
@tarsala1995
@tarsala1995 4 жыл бұрын
The reason for max(0, left/right) is because we don't want to decrease our current solution. If the left subtree is less than 0, then it only makes our solution worse, therefore its better to start from beggining (0). The last statement is for choosing better sum from tree. We have to have a path (not sum of paths) so the greater value the better
@pauljustin9583
@pauljustin9583 4 жыл бұрын
​@@tarsala1995 A single node can form the answer. When all numbers are negative except one, then that non-negative node should be the answer. If all numbers are negative then the biggest of them all should form the answer. This 0 is taking care of those cases. Precisely the next statement where the actual global answer is getting updated. Although this is a poor explanation for this problem, when he went "boom, boom, boom", my head went "fuq, fuq, fuq".
@wolfroma123
@wolfroma123 4 жыл бұрын
Problem is good, explanation could be better though.
@jennaalden3658
@jennaalden3658 3 жыл бұрын
"its easy what more do I need to explain to you?" does not a good teacher make.
@vroomerlifts
@vroomerlifts 6 ай бұрын
These videos are made after you atleast attempt a question, not to learn a question.
@aatifnazar1766
@aatifnazar1766 4 жыл бұрын
Thats a good problem. Normally we dont traverse like this, i mean updating one local max and one global max at the same time is kind of new for me. Great solution. Opens a new way of looking at tree traversals.
@priyanshu.tiwari
@priyanshu.tiwari 4 жыл бұрын
yeah same for me. Brilliant approach
@psn999100
@psn999100 5 жыл бұрын
Thanks Nick. I tried this without watching the solution, and kept getting errors because of the possible negative numbers in the nodes. I think the key piece of code is in line numbers 20 and 21. Since we take a max of 0 and left or right subtree values, we effectively discard any -ve numbers (because they can never make an even bigger path val). Without these max functions in line 20 and 21, this would work just fine for all BT with nodes as positive values. In fact the original interview question is to assume that all nodes have positive values and then the interviewer asks you to modify your code if there were negative numbers. The recursion and all the other calculations can be thought of in 10 mins assuming the candidate has a good grasp at recursion and graph traversals.
@tvishathakur8947
@tvishathakur8947 3 жыл бұрын
Me too! I also kept getting wrong answers for that but still didn't quite understand why does that work when we take max of 0 and right/left sub-tree. What if all the nodes are negative? :(
@psn999100
@psn999100 3 жыл бұрын
@@tvishathakur8947 you can draw out a tree and see why taking 0 is better than including a -ve number in your max path sum.. a -ve number can never increase a path sum. If all numbers are negative, then you need to ask the interviwer what should you return. Those are good clarifying questions actually !
@tvishathakur8947
@tvishathakur8947 3 жыл бұрын
@@psn999100 Yes now I understood! So here in case of negative values it returns the root value. Thankyou! :)
@Garensonic
@Garensonic 2 ай бұрын
I was asked this same question in my Amazon onsite 5 years ago and could not figure out the code that was actually updating the max path sum at the time
@ocoocososococosooooosoococoso
@ocoocososococosooooosoococoso 2 жыл бұрын
so funny when ur say "fun things going in this recursive function" and second later "I don't even know what I'm saying" LOL
@mikami5799
@mikami5799 4 жыл бұрын
i dont get it, what if the root node is positive values? wouldnt this solution be 9+root+42? and how could this answer be possible if no U turn is allowed for paths?
@mohit_talniya
@mohit_talniya 4 жыл бұрын
What if all the Nodes have negative values? You are discarding negative values.
@Cvarier-channel
@Cvarier-channel 3 жыл бұрын
No, the algo would just return the negative value closest to 0.
@jeremycohn9992
@jeremycohn9992 5 жыл бұрын
I'm glad you pointed out there's no backtracking. The original problem didn't say that, but its obvious from the solution that this is the case.
@moonman239
@moonman239 Ай бұрын
so basically we look at any conceivable path in the binary tree, and the maximum sum of all possible paths is the answer?
@sergekov3804
@sergekov3804 4 жыл бұрын
the max space Is O(log n) as your recursion (stack) will never exceed log n. Before start for every of next to each other subtrees previous stack will be cleaned (not true, comments)
@AryanKumar-wn1yd
@AryanKumar-wn1yd 4 жыл бұрын
Not true, in a binary tree the worst- case height is n not log n. Again, WORST CASE height!!
@prashanttripathi7339
@prashanttripathi7339 4 жыл бұрын
O(lg n) would be for a balanced binary tree.
@sergekov3804
@sergekov3804 4 жыл бұрын
I see. You are right guys. Also, I think that is worth mentioning to the interviewer, the balanced and worst cases.
@sjy269
@sjy269 4 жыл бұрын
I think you are confused, man.
@anuragv400
@anuragv400 4 жыл бұрын
Solution is good but you are looking too confused man... I think your explanation made it more complicated
@shubham_only
@shubham_only 4 жыл бұрын
Your clap hurted my ear
@crazytimes564
@crazytimes564 3 жыл бұрын
"you know what I mean" - no we don;t
@AruneshSrivastava
@AruneshSrivastava 4 жыл бұрын
why we need to pass 0 for left and right variables
@ajinkya90swim
@ajinkya90swim 4 жыл бұрын
we compare the with zero because, if we get a negative value from left or right subtree, then we need to just consider the current node in order to maximize the sum. Example : [2 , -1] is your tree, where -1 is the left/right child. If comparison with zero is missing then left = -1, right = 0, curr = 2 , then maxVal = 1, which is wrong. The correct answer is 2. Hence we discard the negative values.
@deveeshmusic5045
@deveeshmusic5045 4 жыл бұрын
your other videos are fine, this is one terrible.
@satyabusi7344
@satyabusi7344 5 жыл бұрын
what was the class that he mentioned. The captions read "walkman with alice videos on algorithms"
@NaveenKumar-os8dv
@NaveenKumar-os8dv 2 жыл бұрын
I had problem understand at that return statement
@AmolGautam
@AmolGautam 2 жыл бұрын
Thank you.
@abhinavyel166
@abhinavyel166 4 жыл бұрын
Can anyone help me with why is he returning result and calculating max_path_sum
@arungiri8560
@arungiri8560 4 жыл бұрын
The max_path_sum indicates the max sum found overall, and the result is sum for the current node, at a certain node how can you decide that whether you should take the left node, or the right node or sum of all three is your answer. For eg. if the nodes were 15--20--7 then we take the max sum as 15+20+7-->max_path_sum but at this point we don't know this is the max sum for sure, but at this point we know the max sum that can be transferred is 15+20-->max(left,rigth)+current.
@ramizrizwan3057
@ramizrizwan3057 Жыл бұрын
great video
@zixuanzhang5950
@zixuanzhang5950 4 жыл бұрын
Great solution! Thanks!
@ShanuGandhi
@ShanuGandhi 4 жыл бұрын
love the way he claps!!!
@aryanjangra4612
@aryanjangra4612 Жыл бұрын
great solution man, although your explanation was horrible (im sorry P) just looking at your solution and dry running it on a random tree did it for me! maintaining two variables/return in a single function is new for me
@444not
@444not 3 жыл бұрын
You clearly don’t know how it works.
@iskandaratakhodjaev9479
@iskandaratakhodjaev9479 4 жыл бұрын
He is just writing the code and calling it explanation of the problem.. lol
@lifehacks9450
@lifehacks9450 4 жыл бұрын
pls uplod unique path 3 problem on leetcode
@vk1618
@vk1618 4 жыл бұрын
Review
@aatifnazar8203
@aatifnazar8203 4 жыл бұрын
please work on your presentation skills, you seem you are not at all interested, just doing it because someone is forcing you to do
@biswamohandwari6460
@biswamohandwari6460 4 жыл бұрын
Man! You look good, talented and to be honest your coding skills are fantastic....
@acxd
@acxd 4 жыл бұрын
nick you should also code in C++
Binary Tree Maximum Path Sum - DFS - Leetcode 124 - Python
15:19
LeetCode 697. Degree of an Array (Algorithm Explained)
12:06
Nick White
Рет қаралды 21 М.
SIZE DOESN’T MATTER @benjaminjiujitsu
00:46
Natan por Aí
Рет қаралды 3,1 МЛН
Как Я Брата ОБМАНУЛ (смешное видео, прикол, юмор, поржать)
00:59
Мама у нас строгая
00:20
VAVAN
Рет қаралды 11 МЛН
УДИВИЛ ВСЕХ СВОИМ УХОДОМ!😳 #shorts
00:49
Best foods around the world 🌍 🤤 #explore #travel
0:15
Paradise Path
Рет қаралды 15 М.
Binary Tree Maximum Path Sum (Animated Walkthrough) (LeetCode)
11:43
AlgosWithMichael
Рет қаралды 23 М.
L17. Maximum Path Sum in Binary Tree | C++ | Java
17:50
take U forward
Рет қаралды 363 М.
I Got Rejected (again)
9:43
Nick White
Рет қаралды 205 М.
Binary tree maximum path sum | Leetcode #124
15:23
Techdose
Рет қаралды 55 М.
LeetCode 98. Validate Binary Search Tree (Algorithm Explained)
9:48
Binary Tree Max Path Sum (LeetCode Day 29)
9:33
Errichto Algorithms
Рет қаралды 28 М.
Self Taught Programmers... Listen Up.
11:21
Nick White
Рет қаралды 1,1 МЛН
SIZE DOESN’T MATTER @benjaminjiujitsu
00:46
Natan por Aí
Рет қаралды 3,1 МЛН