Topic Description:
In a tree full of two. The sort tree depth is k, the node number is 2^k-1, the node value is 1 to (2^k-1), the value of K and any three nodes is given, and the root node of the most young tree containing the three nodes is output.
Sample input: 4 10 15 13
Sample output: 12
First, let's take a look at the two-fork sort tree, which is a 4-story two-fork sort tree:
As can be seen from the diagram above, the sequence traversal of a two-fork sort tree is an increment from 1 to 2^k-1 (k is a layer). So, as long as we give the layer number we can determine the binary sort tree. At the same time, the observation shows that the binary sort tree from the top to bottom of the root node is just a binary lookup of all elements of the middle node.
According to the above rules to solve the three nodes of the most small tree root node of the problem can be the following points:
No need to create a two-fork tree, from the 1-2^k-1 of an ascending array that is a full two-fork sort tree
When the input of three elements on both sides of the two-node, the current binary node is to find the root node of the youngest tree (according to this rule, we do not need to judge three elements, only to determine the maximum element and the minimum element position)
When the value of the minimum node is greater than the value of the two-point node, it continues to look in the right half of the second
When the value of the maximum node is less than the value of the two-point node, it continues to look in the left half of the second part
Based on the following, write Python code as follows:
k, A, b, c = map (int, raw_input (). Split ()) print k,a,b,c MaxValue = max (a,b,c) minvalue = min (a,b , c) left = 1 right = POW (2, K)-1 middle = (left + right)/2 while True:if MinValue < Middle and Middle < Maxvalu E:break if middle < Minvalue:left = middle + 1 middle = (left + right)/2 elif MIDDL E > maxvalue:right = middle-1 middle = (left + right)/2 print middle