You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// You are given the root of a binary tree with n nodes where each node in the tree has node.val coins. There are n coins in total throughout the whole tree.
3
+
// In one move, we may choose two adjacent nodes and move one coin from one node to another. A move may be from parent to child, or from child to parent.
4
+
// Return the minimum number of moves required to make every node have exactly one coin.
5
+
6
+
// Example 1:
7
+
// Input: root = [3,0,0]
8
+
// Output: 2
9
+
// Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
10
+
// Example 2:
11
+
// Input: root = [0,3,0]
12
+
// Output: 3
13
+
// Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.
14
+
15
+
// Constraints:
16
+
17
+
// The number of nodes in the tree is n.
18
+
// 1 <= n <= 100
19
+
// 0 <= Node.val <= n
20
+
// The sum of all Node.val is n.
21
+
22
+
/**
23
+
* Definition for a binary tree node.
24
+
* public class TreeNode {
25
+
* public int val;
26
+
* public TreeNode left;
27
+
* public TreeNode right;
28
+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
0 commit comments