L2-006 树的遍历 (25分) java

给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列。这里假设键值都是互不相等的正整数。

输入格式:

输入第一行给出一个正整数NNN≤30\le 3030),是二叉树中结点的个数。第二行给出其后序遍历序列。第三行给出其中序遍历序列。数字间以空格分隔。

输出格式:

在一行中输出该树的层序遍历的序列。数字间以1个空格分隔,行首尾不得有多余空格。

输入样例:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

   
   

输出样例:

4 1 6 3 5 7 2

   
   
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import javax.management.monitor.Monitor;


//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add ceck for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
	static boolean hasNext()throws IOException {
		return tokenizer.hasMoreTokens();
	}
	static String nextLine() throws IOException{
		return reader.readLine();
	}
	static char nextChar() throws IOException{
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}
	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}
	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}
	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}
	static void close() throws IOException {
		reader.close();
	}
}
class Writer{
	static BufferedWriter writer;
	static void init(OutputStream outputStream) {
		writer = new BufferedWriter(new OutputStreamWriter(outputStream));
	}
	static void print(Object object) throws IOException {
		writer.write(object.toString());
	}
	static void println(Object object) throws IOException {
		writer.write(object.toString());
		writer.write("\n");
	}
	static void close() throws IOException {
		// TODO Auto-generated method stub
		writer.close();
	}
}
public class Main {
	
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		Writer.init(System.out);
		solve();
		Reader.close();
		Writer.close();
	}
	static class Node{
		int data;
		Node left = null;
		Node right=null;
		public Node(int data) {
			this.data = data;
		}
	}
	static int[]aft;
	static int[]mid;
	private static void solve() throws IOException {
		int n = Reader.nextInt();
		aft = new int[n];
		mid = new int[n];
		for (int i = 0; i < n; i++) {
			aft[i] = Reader.nextInt();
		}
		for (int i = 0; i < n; i++) {
			mid[i] = Reader.nextInt();
		}
		Node root = create(0, n-1, 0, n-1);
		cengOrder(root);
	}
	static Node create(int aftLeft,int aftRight,int midLeft,int midRight) {
		if (aftLeft>aftRight) {
			return null;
		}
		Node root = new Node(aft[aftRight]);
		
		// 从中序遍历中找出根节点
		int index = -1;
		for (int i = midLeft; i <= midRight; i++) {
			if (mid[i] == root.data) {
				index = i;
				break;
			}
		}
		// 计算左子树的数量
		int leftNum = index-midLeft;
		root.left = create(aftLeft,aftLeft+leftNum-1,midLeft,index);
		root.right = create(aftLeft+leftNum,aftRight-1,index+1,midRight);
		
		return root;
	}
	static void preOrder(Node root) throws IOException {
		if (root!=null) {
			Writer.println(root.data);
			preOrder(root.left);
			preOrder(root.right);
		}
	}
	static void cengOrder(Node root) throws IOException {
		Queue<Node>q = new LinkedList<Node>();
		q.add(root);
		int index = 0;
		while (!q.isEmpty()) {
			Node newNode = q.poll();
			if (index++==0) {
				Writer.print(newNode.data);
			}else {
				Writer.print(" "+newNode.data);
			}
			
			if (newNode.left!=null) {
				q.add(newNode.left);
			}
			if (newNode.right!=null) {
				q.add(newNode.right);
			}
			
		}
		
	}
}

发布了80 篇原创文章 · 获赞 10 · 访问量 4032

猜你喜欢

转载自blog.csdn.net/weixin_43888039/article/details/104899018