Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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 {
- public:
- int getValue(TreeNode *root) {
- if(root == NULL)
- return 0;
- int val = getValue(root->left) + getValue(root->right) + (root->val) - 1;
- return val;
- }
- int distributeCoins(TreeNode* root) {
- if(root == NULL)
- return 0;
- int val = abs(getValue(root->left));
- val+= abs(getValue(root->right));
- return val + distributeCoins(root->left) + distributeCoins(root->right);
- }
- };
Add Comment
Please, Sign In to add comment