[PAT] 1020 Tree Traversals (25 分)Java

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end 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

 1 package pattest;
 2 
 3 import java.util.LinkedList;
 4 import java.util.Scanner;
 5 
 6 /**
 7  * @Auther: Xingzheng Wang
 8  * @Date: 2019/2/28 15:52
 9  * @Description: pattest
10  * @Version: 1.0
11  */
12 public class PAT1020 {
13     static Scanner scanner = new Scanner(System.in);
14 
15     static class Node {
16         int value;
17         Node left;
18         Node right;
19 
20         Node(int value) {
21             this.value = value;
22         }
23 
24         @Override
25         public String toString() {
26             return "Node{" + "value=" + value + ", left=" + left + ", right=" + right + '}';
27         }
28     }
29 
30     private static Node bulidTree() {
31         int size = scanner.nextInt();
32         int[] postOrder = new int[size];
33         int[] inOrder = new int[size];
34         for (int i = 0; i < size; i++) {
35             postOrder[i] = scanner.nextInt();
36         }
37         for (int i = 0; i < size; i++) {
38             inOrder[i] = scanner.nextInt();
39         }
40         Node root = build(postOrder, inOrder, 0, size - 1, 0, size - 1);
41         return root;
42     }
43 
44     private static Node build(int[] postOrder, int[] inOrder, int postStart, int postEnd, int inStart, int inEnd) {
45         if (postStart > postEnd) {
46             return null;
47         }
48         if (postStart == postEnd) {
49             return new Node(postOrder[postStart]);
50         }
51         int root = postOrder[postEnd--];
52 
53         //find root in inOrder
54         int inIndex = -1;
55         for (int i = inStart; i <= inEnd; i++) {
56             if (root == inOrder[i]) {
57                 inIndex = i;
58                 break;
59             }
60         }
61         //recursion build
62         int leftSize = inIndex - inStart;
63         int rightSize = inEnd - inIndex;
64         Node rootNode = new Node(root);
65         rootNode.left = build(postOrder, inOrder, postStart, postStart + leftSize - 1, inStart, inIndex - 1);
66         rootNode.right = build(postOrder, inOrder, postEnd - rightSize + 1, postEnd, inIndex + 1, inEnd);
67         return rootNode;
68     }
69 
70     public static void main(String[] args) {
71         Node root = bulidTree();
72         LinkedList<Node> queue = new LinkedList<>();
73         queue.add(root);
74         while (!queue.isEmpty()) {
75             Node poll = queue.poll();
76             if (poll.left != null) {
77                 queue.add(poll.left);
78             }
79             if (poll.right != null) {
80                 queue.add(poll.right);
81             }
82             System.out.printf("%d%s", poll.value, queue.isEmpty() ? "\n" : " ");
83         }
84     }
85 }

 

posted @ 2019-03-08 20:23  Pure_Java  阅读(230)  评论(0编辑  收藏  举报