Enter the depth of a two-fork tree and its three nodes, and find the root node of the three-node tree.

Source: Internet
Author: User

For example: the input depth is 4, the three subnodes are 10,13,15, and the minimum subtree is 12.

We can infer that this is a median traversal value from 1 to 2^k-1 according to the tree traversal method.

We can use the binary search technology to find the root node of the oldest tree.

[CPP]  View plain  copy//based on two-point search    #include <algorithm>   #include <cmath>    Int findmin (int* arr,int left,int right)    {        int mid= ((right-left) >>1) +left;       if (arr[0]<=mid& &arr[2]>=mid)            return mid;       else if (arr[0]>mid)             return findmin (arr,mid+1,right);       else if (Arr[2]<mid)            return findmin (arr,left,mid-1);  }    void test2 ()    {       int k=0;        cin>>k;       int arr[3];       &nbsP;for (int i=0;i<3;++i)        {            cin>>arr[i];         }        sort (arr,arr+3);       int right= (1<<k) -1; //move 1 to the left K-bit, = = = "2^k       int min=findmin (arr,1,right);        cout<<min<<endl;  }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.