Advertisement
ogv

Untitled

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