// Title description:
// Given a binary tree's preorder (root, left, right) and middle order (left, root, right) print results, output the binary tree print results by layer (from left to right).
// For example, a binary tree preorder: 1 2 4 5 3; middle order: 4 2 5 1 3. You can build the binary tree shown in the following figure:
//
//
// The result printed by layer is: 1 2 3 4 5
//
// enter
// The first line has only one number, which indicates the number of nodes in the binary tree n (1 <= n <= 1000);
// The second line is a sequence of integers (separated by spaces) consisting of a1, a2, ..., an (1 <= ai <= 1000) —indicating the previous print result;
// The third line is a sequence of integers (separated by spaces) consisting of b1, b2, ..., bn (1 <= bi <= 1000) —indicating the middle-order printing result.
//
// output
//c1,c2,...,cn, separated by spaces—indicates the results printed by layer.
//
//
// Sample input
// 5
// 1 2 4 5 3
// 4 2 5 1 3
//
// Sample output
// 1 2 3 4 5
class Worker {
/ * Array length * /
int ILength;
/ * Number of words already in the result array * /
int ITreeNum = 0;
/ * The leftmost subscript of the new tree in the result array * /
int INewTreeLeft = -1;
/ * The rightmost subscript of the new tree in the result array * /
int INewTreeRight = 0;
public Worker () {
builder = new StringBuilder ("");
}
/ * Assign ILength based on the first input line * /
public void setLength (String s) {
int lenght = 0;
tempC = s.toCharArray ();
for (int i = tempC.length-1, j = 0; i> = 0; i-, j ++) {
lenght + = (tempC [i]-'0') * chengfang (j);
}
ILength = lenght;
}
/ * Initialize the pre-order and pre-order arrays based on the strings entered in the second and third lines, and create the resulting sequence array
public void init (String s0, String s1) {
int i = 0, j = 0;
qian = new String [ILength];
zhong = new String [ILength] [2];
result = new int [ILength] [3];
tempC = s0.toCharArray ();
int length = s0.length ();
for (i = 0, j = 0; i <length; i ++) {
if (tempC [i]! = '') {
builder.append (tempC [i]);
if (i == length -1) {
qian [j] = builder.toString ();
builder.delete (0, builder.capacity ());
}
} else {
if (! builder.toString (). trim (). equals (null)) {
qian [j] = builder.toString ();
builder.delete (0, builder.capacity ());
j ++;
}
}
}
tempC = s1.toCharArray ();
for (i = 0, j = 0; i <length; i ++) {
if (tempC [i]! = '') {
builder.append (tempC [i]);
if (i == length -1) {
zhong [j] [0] = builder.toString ();
builder.delete (0, builder.capacity ());
}
} else {
if (! builder.toString (). trim (). equals (null)) {
zhong [j] [0] = builder.toString ();
builder.delete (0, builder.capacity ());
j ++;
}
}
}
for (i = 0; i <ILength; i ++) {
zhong [i] [1] = "0";
result [i] [0] = 0;
result [i] [1] = 0;
result [i] [2] = 0;
}
}
/ * Enter the index value of the pre-order array target, and output the index value of the target in the mid-order array
public int findZhongAdd (int index) {
if (index> = ILength) {
return -1;
}
String s = qian [index];
for (int i = 0; i <ILength; i ++) {
if (s.equals (zhong [i] [0])) {
return i;
}
}
return -1;
}
/ * Enter the index value of the target in the in-order array and calculate the number of left subtrees of the target * /
public int countLeftNum (int index) {
int num = 0;
while ((index-1)> = 0 && (zhong [index-1] [1] .equals ("0"))) {
index--;
num ++;
}
return num;
}
/ * Enter the subscript value of the right subtree to determine whether it is valid * /
public boolean checkRight (int rightAddr) {
if ((rightAddr> = 0) && (rightAddr <ILength) && zhong [rightAddr] [1] .equals ("0"))
return true;
return false;
}
/ * Calculate the power of 10 * /
public int chengfang (int i) {
int result = 1;
if (i == 0)
return result;
for (int ii = 0; ii <i; ii ++) {
result * = 10;
}
return result;
}
/ *
* Input the index of the result array, and process the left and right subtrees
* /
public void xxx (int i) {
int root = 0;
int left = 0;
int right = 0;
int temp = 0;
int num = 0;
if (result [i] [1]! = -1) {
root = result [i] [1];
temp = findZhongAdd (result [i] [1]);
num = countLeftNum (temp);
if (num> 0) {
left = root + 1;
right = left + num;
} else {
left = -1;
right = root + 1;
}
if (! checkRight (findZhongAdd (right))) {
right = -1;
}
result [ITreeNum] [0] = root;
result [ITreeNum] [1] = left;
result [ITreeNum] [2] = right;
ITreeNum ++;
zhong [temp] [1] = "1";
if (left! = -1) {
zhong [findZhongAdd (left)] [1] = "1";
}
if (right! = -1) {
zhong [findZhongAdd (right)] [1] = "1";
}
}
if (result [i] [2]! = -1) {
root = result [i] [2];
temp = findZhongAdd (result [i] [2]);
num = countLeftNum (temp);
if (num> 0) {
left = root + 1;
right = left + num;
} else {
left = -1;
right = root + 1;
}
if (! checkRight (findZhongAdd (right))) {
right = -1;
}
result [ITreeNum] [0] = root;
result [ITreeNum] [1] = left;
result [ITreeNum] [2] = right;
ITreeNum ++;
zhong [temp] [1] = "1";
if (left! = -1) {
zhong [findZhongAdd (left)] [1] = "1";
}
if (right! = -1) {
zhong [findZhongAdd (right)] [1] = "1";
}
}
}
/ * Output result array * /
public void doit () {
int temp = 0;
int rootLoc = 0;
int leftLoc = 0;
int rightLoc = 0;
int num = 0;
while (INewTreeLeft <(ILength-1)) {
if (INewTreeLeft == -1) {
rootLoc = 0;
temp = findZhongAdd (0);
num = countLeftNum (temp);
if (num> 0) {
leftLoc = rootLoc + 1;
rightLoc = leftLoc + num;
} else {
leftLoc = -1;
rightLoc = rootLoc + 1;
}
if (! checkRight (findZhongAdd (rightLoc)))) {
rightLoc = -1;
}
result [ITreeNum] [0] = rootLoc;
result [ITreeNum] [1] = leftLoc;
result [ITreeNum] [2] = rightLoc;
ITreeNum ++;
zhong [temp] [1] = "1";
if (leftLoc! = -1) {
zhong [findZhongAdd (leftLoc)] [1] = "1";
}
if (rightLoc! = -1) {
zhong [findZhongAdd (rightLoc)] [1] = "1";
}
} else {
xxx (INewTreeLeft);
}
INewTreeLeft ++;
}
}
public void printLength () {
System.out.println (ILength);
}
public void printQianZhong () {
for (int i = 0; i <ILength; i ++) {
System.out.print (qian [i] + "");
}
System.out.println ();
for (int i = 0; i <ILength; i ++) {
System.out.print (zhong [i] [0] + "");
}
System.out.println ();
for (int i = 0; i <ILength; i ++) {
System.out.print (zhong [i] [1] + "");
}
System.out.println ();
}
public void printResult () {
for (int i = 0; i <ILength; i ++) {
System.out.print (result [i] [0] + "");
}
System.out.println ();
for (int i = 0; i <ILength; i ++) {
System.out.print (result [i] [1]
+ "");
}
System.out.println ();
for (int i = 0; i <ILength; i ++) {
System.out.print (result [i] [2] + "");
}
System.out.println ();
}
}
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.