Files
C/leetcode/src/979.c
Alexander Pantyukhin 55f73501ea feat: add Distribute Coins in Binary Tree LeetCode (#1206)
* add leetcode Distribute Coins in Binary Tree

* updating DIRECTORY.md

* Update 979.c

fix review notes

* Update leetcode/src/979.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update leetcode/src/979.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update leetcode/src/979.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update leetcode/src/979.c

Co-authored-by: David Leal <halfpacho@gmail.com>

---------

Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
2023-02-03 18:11:43 -06:00

48 lines
1.5 KiB
C

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct NodeDistributeInfo {
int distributeMoves;
int distributeExcess;
};
struct NodeDistributeInfo* getDisturb(struct TreeNode* node) {
struct NodeDistributeInfo* result = malloc(sizeof(struct NodeDistributeInfo));
if (node == NULL) {
result->distributeMoves = 0;
result->distributeExcess = 1;
return result;
}
struct NodeDistributeInfo* leftDistribute = getDisturb(node->left);
struct NodeDistributeInfo* rightDistribute = getDisturb(node->right);
int coinsToLeft = 1 - leftDistribute->distributeExcess;
int coinsToRight = 1 - rightDistribute->distributeExcess;
// Calculate moves as excess and depth between left and right subtrees.
result->distributeMoves = leftDistribute->distributeMoves + rightDistribute->distributeMoves + abs(coinsToLeft) + abs(coinsToRight);
result->distributeExcess = node->val - coinsToLeft - coinsToRight;
free(leftDistribute);
free(rightDistribute);
return result;
}
// Depth-first search .
// On each node-step we try to recombinate coins between left and right subtree.
// We know that coins are the same number that nodes, and we can get coins by depth
// Runtime: O(n)
// Space: O(1)
int distributeCoins(struct TreeNode* root) {
return getDisturb(root)->distributeMoves;
}