979. Distribute Coins in Binary Tree
一次过有点意外 不过我也只写得动medium了==
从leaf node 算盈余 每多一个或少一个都要移动硬币
class Solution {
public:
int distributeCoins(TreeNode* root) {
int ans = 0;
dfs(root, ans);
return ans;
}
int dfs(TreeNode* root, int &ans){
if(root == nullptr)
return 0;
int left = dfs(root->left, ans);
int right = dfs(root->right, ans);
ans += abs(left) + abs(right);
return left + right + root->val - 1;
}
};
话说我想开始学rust有人有建议ㄇ