-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path979.cpp
29 lines (29 loc) · 863 Bytes
/
979.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
// Runtime: 8 ms, faster than 88.79% of C++ online submissions for Distribute
// Coins in Binary Tree. Memory Usage: 13.2 MB, less than 59.61% of C++ online
// submissions for Distribute Coins in Binary Tree.
public:
int distributeCoins(TreeNode *root) {
int ret = 0;
function<int(TreeNode *)> recursion = [&ret, &recursion](TreeNode *root) {
if (!root)
return 0;
auto leftNeed = recursion(root->left);
auto rightNeed = recursion(root->right);
ret += abs(leftNeed) + abs(rightNeed);
root->val -= leftNeed + rightNeed;
return 1 - root->val;
};
recursion(root);
return ret;
}
};