Company: Tower Research_22july
Difficulty: medium
Balanced Tree Problem Description A tree is considered balanced when the absolute difference between the number of stones on any pair of adjacent nodes is at most 1. Given a rooted tree with n nodes (numbered 1 to n), each node has some number of stones. The goal is to modify this tree by adding stones to make it a balanced tree. Find the minimum number of extra stones needed to balance a tree. Examples Example 1: Consider the following tree: Input Tree: Node 1: Stones 1 Node 2: Stones 4 Node 3: Stones 2 Node 4: Stones 6 Node 5: Stones 5 Edges: (1,2), (2,3), (2,4), (2,5) Modified Tree: Node 1: Stones 4 Node 2: Stones 5 Node 3: Stones 4 Node 4: Stones 4 Node 5: Stones 5 Explanation: In the provided tree, the optimal way to balance it is: Add 2 stones to node 3 Add 1 stone to node 2 Add 4 stones to node 1 After these additions, the tree becomes balanced, and the minimum number of stones required is 2 + 1 + 4 = 7. Example 2: Input: tree_nodes = 5, m = 4 tree_from = [1, 2, 2, 2], tree_to =