Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- Map<TreeNode, Integer> balance;
- int moves;
- public int distributeCoins(TreeNode root) {
- balance = new HashMap<TreeNode, Integer>();
- count(root);
- normalize(root);
- return moves;
- }
- private void normalize(TreeNode root) {
- if (root == null) return;
- normalize(root.left);
- normalize(root.right);
- }
- private int count(TreeNode root) {
- if (root == null) return 0;
- int b = root.val - 1 + count(root.left) + count(root.right);
- balance.put(root, b);
- return b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement