You are given the root
of a binary tree with n
nodes where each node
in the tree has node.val
coins and there are n
coins total.
In one move, we may choose two adjacent nodes and move one coin from one node to another. (A move may be from parent to child, or from child to parent.)
Return the number of moves required to make every node have exactly one coin.
Example 1:
Input: root = [3,0,0]
Output: 2
Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
Example 2:
Input: root = [0,3,0]
Output: 3
Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
Example 3:
Input: root = [1,0,2]
Output: 2
Example 4:
Input: root = [1,0,0,null,3]
Output: 4
Constraints:
- The number of nodes in the tree is
n
. 1 <= n <= 100
0 <= Node.val <= n
- The sum of
Node.val
isn
.
這道題給了一棵二叉樹,說是結點值代表硬幣的個數,且每個結點可以給其相連的子結點或者父結點傳硬幣,現在假設硬幣的總個數和結點的總個數相同,但是分布不均勻,問需要移動多少次硬幣可以使每個結點上正好只有一個硬幣。這道題其實難度還不小,關鍵看腦子能否轉過一個彎來,一般人可以開始會從根結點來分析,假如硬幣值大於1了,知道有多余的硬幣可以傳給其他的結點,如果是1,就不用變,若是0,表示需要從子結點傳個結點過來,若子結點有多余的硬幣還好說,若子結點也是0呢,那么子結點本身也需要別人給傳硬幣,這個硬幣還有可能是從根結點的另一個子樹中的某個位置傳過來。感覺一下子好混亂,找不出規律,瞬間失去了思路,有時候從根結點分析不出來的話,就要試試從葉結點開始分析,就像之前那道 Binary Tree Cameras 一樣,因為葉結點沒有子結點了,它要是硬幣不夠,只能從父結點獲得,它要是多余了硬幣,也只能傳給唯一的父結點(除非該葉結點就是根結點)。
不管是給還是要,都是算一次移動,本質沒有太大的區別,不需要分開統計,直接加在一起就行。為了方便起見,就當作每個結點都會給出當前結點值減1個的硬幣,若當前是0的話,就給出 -1 個,其實就是要一個。這樣每個結點可以給出的硬幣的總個數就是左右子結點分別可以給出的個數加上當前結點值並減1,這就找出規律了,而且根據遍歷的順序可以知道這是二叉樹的后序遍歷,不管什么順序的遍歷,用遞歸來寫都很簡單。在遞歸函數中,先判空,若為空則返回0。否則分別對左右子結點調用遞歸函數並將結果保存在變量 left 和 right 中,返回值就是要給出的硬幣個數,由於可能存在負數,表示需要得到,但是不管得到還是給出都是移動,所以將二者的絕對值加起來,再加到結果 res 中,然后返回當前結點需要給出的硬幣總數,前面也提到了,就是左右子結點分別可以給出的個數加上當前結點值並減1,參見代碼如下:
class Solution {
public:
int distributeCoins(TreeNode* root) {
int res = 0;
helper(root, res);
return res;
}
int helper(TreeNode* node, int& res) {
if (!node) return 0;
int left = helper(node->left, res), right = helper(node->right, res);
res += abs(left) + abs(right);
return node->val + left + right - 1;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/979
類似題目:
參考資料:
https://leetcode.com/problems/distribute-coins-in-binary-tree/