- Distribute Coins in Binary Tree
Please help me solving this problem,I cannot.
Please help me solving this problem,I cannot.
@Somasree as we are given that the tree will have n coins and we only need to arrange then so that every node have equal coin
so for every node if we have a shortage of coins in the subtree of that node we that and if we have excessive coins then we also take it as extra steps that coin will make and then
we go up from this subtree and return this count variable( it consists of both factors wehther the coins are less or excessive we need to travel that edge that difference of coins no of times
Coding Blocks IDE
please see this and if it is not clear let me know…
root->val+left_difference+right_difference-1
can u plz tellme why is it -1
@Somasree as we have to place one coin in this node also and we can place it from root->val
for example
we have left ans and right ans for left and right subtrees and
we see (root->val-1) as extra coins in this node ans this value can also be negative if we do not have coins in this node and then we return (left+right+(root->val-1))
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.