03-树3 Tree Traversals Again

Posted kuotian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了03-树3 Tree Traversals Again相关的知识,希望对你有一定的参考价值。

push为前序遍历序列,pop为中序遍历序列。将题目转化为已知前序、中序,求后序。

前序GLR 中序LGR

前序第一个为G,在中序中找到G,左边为左子树L,右边为右子树R。

将左右子树看成新的树,同理。

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.

技术分享
Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N(30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop

Sample Output:

3 4 2 6 5 1

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <stack>
 4 #include <string>
 5 using namespace std;
 6 
 7 #define MaxSize 30 
 8 
 9 #define OK 1
10 #define ERROR 0
11 
12 int preOrder[MaxSize];
13 int inOrder[MaxSize];
14 int postOrder[MaxSize];
15 
16 void postorderTraversal(int preNo, int inNo, int postNo, int N);
17 
18 int main()
19 {
20     stack<int> stack;
21     int N;        //树的结点数 
22     cin >> N;
23     string str;
24     int data;
25     int preNo = 0, inNo = 0, postNo = 0; 
26     for(int i = 0; i < N * 2; i++) {        //push + pop = N*2 
27         cin >> str;
28         if(str == "Push") {            //push为前序序列 
29             cin >> data;
30             preOrder[preNo++] = data;
31             stack.push(data);
32         }else{                        //pop出的是中序序列 
33             inOrder[inNo++] = stack.top();
34             stack.pop();            //pop() 移除栈顶元素(不会返回栈顶元素的值) 
35         }
36     }
37     postorderTraversal(0, 0, 0, N);
38     for(int i = 0; i < N; i++) {        //输出后序遍历序列  
39         if(i == 0)                        //控制输出格式 
40             printf("%d",postOrder[i]);
41         else
42             printf(" %d",postOrder[i]);
43     }
44     printf("\n");
45     return 0;
46 }
47 
48 void postorderTraversal(int preNo, int inNo, int postNo, int N) 
49 {
50     if(N == 0)
51         return;
52     if(N == 1) {
53         postOrder[postNo] = preOrder[preNo];
54          return;
55     }
56     int L, R; 
57     int root = preOrder[preNo];            //先序遍历GLR第一个为根 
58     postOrder[postNo + N -1] = root;     //后序遍历LRG最后一个为根
59     for(int i = 0; i < N; i++) {
60         if(inOrder[inNo + i] == root) {    //找到中序的根 左边为左子树 右边为右子树 
61             L = i;                        //左子树的结点数 
62             break;
63         }
64     } 
65     R = N - L - 1;                        //右子树的结点数 
66     postorderTraversal(preNo + 1, inNo, postNo, L);    //同理,将左子树看成新的树 
67     postorderTraversal(preNo + L + 1, inNo + L + 1, postNo + L, R);//同理,右子树 
68 } 

 

 

以上是关于03-树3 Tree Traversals Again的主要内容,如果未能解决你的问题,请参考以下文章

03-树3 Tree Traversals Again (25 分)

03-树3 Tree Traversals Again

03-树3 Tree Traversals Again (25分)

03-树3 Tree Traversals Again (25分)

03-树3 Tree Traversals Again (25 分)

03-树3 Tree Traversals Again (25分)