浙大数据结构pta——03-树3:Tree Traversals Again (25分)

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 1figure1
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

这道题就是一道已知先序和中序然后让你用后序遍历树并输出,push的顺序即为先序,pop的顺序即为中序。那么就需要递归的寻找根节点,先序的第一个元素为根节点,那么中序中找到根节点,根节点的前半部分为左子树,后半部分为右子树,就这样不断递归,然后存入后序中即可,详见代码及注释

比如题目中的例子
先序:1 2 3 4 5 6
中序:3 2 4 1 6 5
后序:3 4 2 6 5 1

#include <iostream>
#include <string>
#include <stack>
#include <vector>

using namespace std;

void GetPostOrder(vector<int> &PreOrder/*先序数组*/,int PreL/*先序指针*/,vector<int> &InOrder/*中序数组*/,int InL/*中序指针*/,
                  vector<int> &PostOrder/*后序数组*/,int PostL/*后序指针*/,int n)
{
    if(n == 0)
    {
        return;
    }
    else if( n == 1)
    {
        PostOrder[PostL] = PreOrder[PreL];
        return;
    }
    int root = PreOrder[PreL];/*先序序列的第一个为根节点*/
    PostOrder[PostL + n - 1] = root;/*后序序列中最后访问根节点*/
    /*在中序序列中找到根节点*/
    int i = 0;
    while(i < n)
    {
        if(InOrder[InL + i] == root)
        {
            break;/*找到对应根节点*/
        }
        i++;
    }
    int L = i;/*左子树*/
    int R = n - i - 1;/*右子树*/
    GetPostOrder(PreOrder,PreL + 1,InOrder,InL,PostOrder,PostL,L);/*遍历左子树*/
    GetPostOrder(PreOrder,PreL + L + 1,InOrder,InL + L + 1,PostOrder,PostL + L,R);/*遍历右子树*/
}

int main()
{
    int n;
    cin >> n;
    vector<int> PreOrder(n,0);
    vector<int> InOrder(n,0);
    stack<int> st;
    int PreL = 0,InL = 0;
    for(int i = 0 ; i < 2 * n; i++)
    {
        string str;
        int temp;
        cin >> str;
        if(str == "Push")
        {
            cin >> temp;
            PreOrder[PreL++] = temp;/*先序序列*/
            st.push(temp);
        }
        else
        {
            InOrder[InL++] = st.top();/*中序序列*/
            st.pop();
        }
    }
    vector<int> PostOrder(n,0);
    GetPostOrder(PreOrder,0,InOrder,0,PostOrder,0,n);
    for(int i = 0 ; i < n ; i++)
    {
        if(i < n - 1)
        {
            cout << PostOrder[i] << " ";
        }
        else
        {
            cout << PostOrder[i] << endl;
        }
    }
    return 0;
}

发布了18 篇原创文章 · 获赞 4 · 访问量 314

猜你喜欢

转载自blog.csdn.net/leslie___/article/details/105669877
今日推荐