LeetCode solution summary 979. Assign coins in a binary tree

Directory link:

Lituo Programming Problems - Summary of Solutions_Share+Records-CSDN Blog

GitHub synchronous brushing project:

https://github.com/September26/java-algorithms

Link to the original title: Likou


describe:

Given  N the root node of a binary tree with one node  root, each node in the tree corresponds to  node.val a coin, and there are a total of  N coins.

In one move, we can select two adjacent nodes and move a coin from one of them to the other. (Movement can be from parent node to child node, or from child node to parent node.).

Returns the number of moves required to get only one coin on each node.

Example 1:

Input: [3,0,0]
 Output: 2
 Explanation: Starting from the root of the tree, we move a coin to its left child and a coin to its right child.

Example 2:

Input: [0,3,0]
 Output: 3
 Explanation: Starting from the left child of the root node, we move two coins to the root node [move twice]. Then, we move a coin from the root node to the right child node.

Example 3:

Input: [1,0,2]
 Output: 2

Example 4:

Input: [1,0,0,null,3]
 Output: 4

hint:

  1. 1<= N <= 100
  2. 0 <= node.val <= N

Problem-solving ideas:

* Problem-solving ideas:

* In fact, this question is very similar to a balanced binary tree.

* We construct two trees,

* The first tree, numMap, records how many coins each node and its child nodes should have.

* The second tree, sumMap, records how many coins each node and its child nodes currently have.

* Each node currently has, minus the number of coins it should have, the absolute value of the difference is the amount that each node should send or accept.

* Finding the sum of this difference is the result we want.

code:

class Solution979
{
public:
    int getTreeNodeNum(TreeNode *node, std::map<TreeNode *, int> &numMap)
    {
        if (node == nullptr)
        {
            return 0;
        }
        int num = 1;
        num += getTreeNodeNum(node->left, numMap);
        num += getTreeNodeNum(node->right, numMap);
        numMap[node] = num;
        return num;
    }

    int getTreeNodeSum(TreeNode *node, std::map<TreeNode *, int> &sumMap)
    {
        if (node == nullptr)
        {
            return 0;
        }
        int sum = node->val;
        sum += getTreeNodeSum(node->left, sumMap);
        sum += getTreeNodeSum(node->right, sumMap);
        sumMap[node] = sum;
        return sum;
    }

    int distributeCoins(TreeNode *root)
    {
        int sum = 0;
        std::map<TreeNode *, int> numMap;
        std::map<TreeNode *, int> sumMap;
        getTreeNodeNum(root, numMap);
        getTreeNodeSum(root, sumMap);
        // std::cout << "Value: " << sumMap.size() << std::endl;
        int result = 0;
        for (auto it = numMap.begin(); it != numMap.end(); ++it)
        {
            int sum = sumMap[it->first];
            int num = it->second;
            result += abs(sum - num);
        }
        return result;
    }
};

Guess you like

Origin blog.csdn.net/AA5279AA/article/details/131721482