Advertisement
ogv

Untitled

ogv
Nov 21st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. class Solution {
  2. Map<TreeNode, Integer> balance;
  3. int moves;
  4.  
  5. public int distributeCoins(TreeNode root) {
  6. balance = new HashMap<TreeNode, Integer>();
  7. count(root);
  8. normalize(root);
  9. return moves;
  10. }
  11.  
  12. private void normalize(TreeNode root) {
  13. if (root == null) return;
  14.  
  15. normalize(root.left);
  16. normalize(root.right);
  17. }
  18.  
  19. private int count(TreeNode root) {
  20. if (root == null) return 0;
  21.  
  22. int b = root.val - 1 + count(root.left) + count(root.right);
  23. balance.put(root, b);
  24. return b;
  25. }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement