2018 Internship-Alibaba internal push programming problem

Source: Internet
Author: User

For a two-fork tree consisting of a decimal integer, if the depth does not exceed 4, it can be represented by an array of three-bit decimal integers, with the following rules:

1, the hundred number indicates that the tree hierarchy l,1<=l<=4; 10 digits represents the position in that level p,1<=p<=8; the digit number represents the value v.

2, the array, L must be monocytogenes, that is, after a number of l greater than or equal to the previous number of L;

3, for the same l,p is also monocytogenes, that is, in the case of L, after a number of p is greater than or equal to the previous number of p.

Example: [113, 215, 221]

   3
  /\
 5   1

Now ask this tree all the way to the leaf node and, for example, the path and for (3+5) + (3+1) =12

The meaning of the title is to give a few three digits, such as 113, which represents a depth of 1, the 1th on the left is 3, 215 is the depth of 2, and the 1th on the left is a value of 5 ...

Examples:
1
/ \
2 3
/ \
4 5
Input:
111
212
223
314
325
Output:
19

The idea of solving problems is:

1. Read the input, split the three-digit number, respectively, with the array bai, array shi, array ge store the values on the corresponding bit

2. Build a binary tree with an array of treearray, preserving the values of GE arrays

3. Iterate through the array treearray, find all the leaf nodes, and then calculate the path length of the root node to the leaf node according to the leaf nodal point.

The code is as follows:

Import java.util.ArrayList;
Import java.util.List;

Import Java.util.Scanner; public class Main {public static void main (string[] args) {//Read input contents list<integer> input = NE
        W arraylist<integer> ();
        Scanner sc = new Scanner (system.in);
        String str = sc.nextline ();
            while (str! = null &&!str.isempty ()) {Integer value = integer.parseint (str);
            if (value==0) break;
            Input.add (value);
        str = Sc.nextline ();
        } int[] A = new int[input.size ()];
        for (int i = 0; i < a.length; i++) {A[i] = Input.get (i). Intvalue ();
        } int res = resolve (A);
    System.out.println (RES);
        } static int Resolve (int[] A) {//three-digit split, respectively with array bai, array shi, array ge store the value of the corresponding bit int len = a.length;
        int[] Bai = new Int[len];
        Int[] Shi = new Int[len];
        int[] ge = new Int[len];
for (int i = 0; i < len; i++) {            Bai[i] = a[i]/100;
            Shi[i] = A[I]%100/10;
        Ge[i] = a[i]%10;
        } final int P = 15;
        int[] Treearray = new Int[p];
        for (int i = 0; i < treearray.length; i++) {treearray[i] = Integer.min_value; }//Build binary tree with array Treearray, save value for GE array for (int i = 0; i < len;i++) {int loc = bai[i]* (bai[i]-1)/2
            +shi[i]-1;
        Treearray[loc] = Ge[i];
        }//Traversal array treearray, find all leaf node int sum = 0; for (int i = 0; i < P; i++) {if (Treearray[i]! = Integer.min_value) {if (i*2+1 >= P | | i*
                2+2&GT;=P) {//is a leaf node sum+=helper (treearray,i); } if (treearray[i*2+1] = = Integer.min_value) {//is a leaf node sum+=helper (
                Treearray,i);
    }}} return sum; }//based on the leaf node, the path length of the root node to the leaf junction is calculated by the static int helper (Int[] Treearray,int index) {//Is the root node if (index = = 0) {return treearray[index];
        } if (index%2==1) {//is the left node return Treearray[index]+helper (Treearray, (index-1)/2);
        }else{//Is Right node return Treearray[index]+helper (Treearray, (index-2)/2); }
    }
}

The code that determines if a leaf node is a point:

if (treearray[i]! = integer.min_value) {
    if (i*2+1 >= P | | i*2+2>=p) {
        //Is leaf node
        sum+=helper (treearray,i );
    }
    if (treearray[i*2+1] = = Integer.min_value) {
        //Is leaf node
        sum+=helper (treearray,i);
    }
}

The first if means: if the index of the current position is I,
Then the index of the left Dial hand node is i*2+1, and the index of the right child node is i*2+2
If the index of the left Dial hand node and the right child node exceeds the length of Treearray, it indicates that the current index i is in the last row, it must be a leaf node.

The second if means: if the first if is satisfied, that is, the current index i is not in the last row, if its left child node is empty, then it must be a leaf node test

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.