Editorial for COCI '06 Contest 4 #5 Jogurt
Submitting an official solution before solving the problem yourself is a bannable offence.
There's more than one way to solve this problem, we present one of them.
Let's try to extend a tree of depth for which the property is satisfied to a tree of depth for which the property is satisfied. Have the root node of the new tree contain the number and put two copies of the old tree as its left and right subtrees, only multiplied by . Now the subtrees contain only even numbers and each appears twice between the two subtrees. The property is still satisfied in the subtrees; multiplying the elements by two caused the differences to be multiplied by two, but the depths of those nodes increased by one so this is good.
Add to all leaf nodes in the left subtree and to all non-leaf nodes in the right subtree. Now each element appears only once in the entire tree. The property is still satisfied in the subtrees because adding a constant to all nodes on the same level in a subtree does not change the differences. The property holds for the root node because we added to the left subtree and to the right subtree so the difference is .
Comments