Title Description:
In a tree with a full two forks, the tree depth is k, the number of nodes is 2^k-1, the node value is 1 to (2^k-1), the values of K and any three nodes are given, and the root node of the minimum 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-layer full two-fork sort tree:
* 8
* / \
* 4
* /\ / \
* 2 6 14
* /\ /\ /\/ \
* 1 3 5 7 9 11 13 15
As can be seen from the above figure, the ordinal traversal of a two-fork sort tree is an ascending sequence from 1 to 2^k-1 (K is the number of layers). So, given the number of layers, we can determine the binary sort tree. At the same time, it is observed that the root node of the binary sort tree from top to bottom is exactly the middle node of the binary lookup of all the elements.
According to the above rules to solve the three nodes of the most small tree root node This problem can be obtained as follows: There is no need to establish a two-fork tree, the increment from the 1-2^k-1 array is a full two fork sorting tree when the input of three elements on both sides of the two nodes, The current binary node is the root node of the youngest tree to find (according to this rule, we do not need to judge three elements, only the maximum element and the smallest element of the position can be determined) when the value of the minimum node is greater than the value of the binary node, then continue in the right half of the second point to find when the value of the maximum node Continue to find in the left half of the two-part
Based on the following, write the code as follows:
Import Java.util.Scanner;
/** * Created by Zhuxinquan on 17-4-4. * For a full two-fork sorting tree depth of k, the number of nodes is 2^k-1; the node value is 1 to (2^k-1), * give the value of K and any three nodes, output the root node of the minimum tree containing the three nodes * 8 */\ * 4 */\/\ * 2 6 */\/\/\/\ * 1 3 5 7 9 * */public class BINARYSO
rttree {public static void main (string[] args) {Scanner Scanner = new Scanner (system.in);
int layer = Scanner.nextint ();
int a = Scanner.nextint ();
int b = Scanner.nextint ();
int c = Scanner.nextint (); int max = a > B?
(A > C a:c): (b > C b:c); int min = a < b?
(A < C A:C): (b < C b:c);
int left = 1;
int right = (int) Math.pow (2, layer)-1;
int middle = (left + right)/2;
while (true) {if (middle > Min && middle < max | | middle = min | | middle = max) {
Break
} if (min > middle) { left = middle + 1;
Middle = (left + right)/2;
}else if (Max < middle) {right = Middle-1;
Middle = (left + right)/2;
}} System.out.println (middle); }
}