1020 Tree Traversals (25)(25 分)
Suppose that all the keys in a binary tree are distinct positiveintegers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of thecorresponding binary tree.
Input Specification:
Each input file contains one test case. For each case, the first linegives a positive integer N (<=30), the total number of nodes in thebinary tree. The second line gives the postorder sequence and the thirdline gives the inorder sequence. All the numbers in a line are separatedby a space.
Output Specification:
For each test case, print in one line the level order traversal sequenceof the corresponding binary tree. All the numbers in a line must beseparated by exactly one space, and there must be no extra space at theend of the line.
Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
Analysis:
According to the inorder and postorder traversal sequences, print the level order traversal sequence of the corresponding binary tree. the last node of the postorder traversal is the root node,then find the node in the inorder traversal ,the nodes on its left are the left subtree,while on its right are the right subtree. use recursion to build the tree,the use bfs to print the level order traversal of the binary tree.
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
class tree{
public tree l=null,r=null;
public int data;
}
public class Main {
public static void main(String[] args) throws IOException {
//System.setIn(new FileInputStream("src/A1020/Untitled 1"));
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
String in[]=bufferedReader.readLine().split(" ");
int n=Integer.parseInt(in[0]);
int ino[]=new int[30];
int posto[]=new int[30];
in=bufferedReader.readLine().split(" ");
for(int i=0;i<n;i++)
posto[i]=Integer.parseInt(in[i]);
in=bufferedReader.readLine().split(" ");
for(int i=0;i<n;i++)
ino[i]=Integer.parseInt(in[i]);
tree root=Build(posto,ino,n);
LinkedList<tree>linkedList=new LinkedList();
linkedList.add(root);
boolean flag=true;
while(!linkedList.isEmpty()) {
tree temp=linkedList.getFirst();
if(flag) {
System.out.printf("%d",temp.data);
flag=false;
}
else {
System.out.printf(" %d",temp.data);
}
linkedList.removeFirst();
if(temp.l!=null)
linkedList.addLast(temp.l);
if(temp.r!=null)
linkedList.addLast(temp.r);
}
}
private static tree Build(int[] posto, int[] ino, int n) {// n stands for the number of the nodes on the correspoding orders
if(n==0)return null;
tree root=new tree();
root.data=posto[n-1];//the last element must be its root
int i;
for(i=0;i<n;i++)
if(ino[i]==posto[n-1])//find the element in the inorder.
break;
root.l=Build(posto, ino, i);//pay attention to the parameters of the function
root.r=Build(Arrays.copyOfRange(posto, i, posto.length), Arrays.copyOfRange(ino, i+1, ino.length), n-i-1);
return root;
}
}