PAT 甲级 1020 Tree Traversals(中序后序遍历转层序)

本文介绍了一种根据二叉树的中序和后序遍历序列构建二叉树,并输出该树的层次遍历序列的方法。通过递归构建二叉树结构,再使用队列实现层次遍历。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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;
	}
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值