Guest User

Untitled

a guest
Jan 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12.  
  13. int getValue(TreeNode *root) {
  14. if(root == NULL)
  15. return 0;
  16. int val = getValue(root->left) + getValue(root->right) + (root->val) - 1;
  17. return val;
  18. }
  19.  
  20. int distributeCoins(TreeNode* root) {
  21. if(root == NULL)
  22. return 0;
  23.  
  24. int val = abs(getValue(root->left));
  25. val+= abs(getValue(root->right));
  26.  
  27. return val + distributeCoins(root->left) + distributeCoins(root->right);
  28. }
  29. };
Add Comment
Please, Sign In to add comment