LeetCode Distribute Coins in a Binary Tree Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Nick,
Instead of just saying "It's just how it is. We just have to use the absolute value", it would be better to explain the actual idea behind this.
So the first thing I would do is to explain what this dfs function does.
If you take a very close look at the dfs() function, it pretty much returns how much coin flux has to happen at the node that you are calling dfs().

For example, dfs(node.right) means how much coin has to move from node and node.right. It can be minus value, which means the coin has to move from node to node.right. Take a look at example of [3, 0, 0] which is the first example in the question. from the root node, the dfs(node.right) returns -1. This means that 1 coin has to move from the root to the root.right. Same thing applies for the root.left.

On the other hand, take a look at the second example, which is [0, 3, 0]. In this example dfs(root.left) would return 2. This means 2 coins have to move from root.left to root.

Now that we have an understanding of what this dfs function does, it's easier to formulate a logic to count the number of moves. Since moving coin from a node to its child node and vice versa both counts as a move, we need the abs() functionality to count the moves. On top of this, we need to keep track of moves that happens at each node in the tree and accrue them. That's why and how abs() function logic works for this problem when you accrue the number of moves using the global variable.

Hopefully my explanation makes sense to you.
I hope you take my feedback and make a video that contains deeper and better explanation.

sjy
Автор

Dude, are you deleting comments??
You aren't able to explain like you do for other questions.

raiyanrazi
Автор

So my last comment was deleted because I was critical of this video. My stance hasn't changed. Nick's other videos are pretty good but this explanation is very poor. There are some excellent posts on leetcode discussion forums that'll be of more help.

johnstephen
Автор

Hi Nick, It would be grateful to all viewers if explanation would have been better. Most of the viewers are here to watch for explanation and understanding rather than the code. We know you code good.

jugsma
Автор

well that was unexpected. I actually solved it by dealing with just a one coin. When I see excess in a node that is close to the bottom I try to donate it to the underlying nodes, if underlying nodes are full, then I pull it to the parent of the excess node. I do this procedure until there is no excess in the tree. And it was accepted, but I think from time complexity it wouldn't be even close to the solution.

IvanNovitskii
Автор

great video!!! Can you actually add videos in to playlists based on category?

tonybernoulli
Автор

Man, I literally want by 8 minutes back I wasted watching this video. You are not here to flaunt your coding skills, you are here to explain the approach. I hope you understand this.

NeverGiveUp
Автор

One of the worst LeetCode video I have ever watched....

yuchengchiang
Автор

how did you get 288k subscribers if you can't figure out basic stuffs.

anshumanaditya
Автор

Video is good; explanation is good:) Thanks man!

akshatpahwa
Автор

are you belong to chemistry background

hanumanaram
Автор

Bug. Look at what is returned from give_coins vs. what is returned from num_moves.

dcd
Автор

you have great vids but should explain the math behind the solution more. that is the only part that is confusing.

rayray
Автор

What's with all the dislikes? The explanation is good :)

mh
join shbcf.ru