1086 Tree Traversals Again (25分)

110 篇文章 0 订阅

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

又是这道建树题,经典经典,回顾了一下又发现了自己几个问题
这题stack的入栈顺序是前序遍历,出站顺序是中序遍历,也就是用这两个来建树。
重点:每次做题都把前序和中序写出来,这样就知道递归时该填的参数是什么。
用while(cin>>caozuo)来读取操作,这样就不用知道有几步操作了。
复习的知识点:1.pop()函数没有返回值,要用top()再pop()
2.用一个unordered_map来存中序序列中每个结点的位置,这样就能通过结点值来找它的下标,方便计算左右子树的长度
3.递归建树是有递归边界的!如果preL大于preR,就返回nullptr。每次建树定义一个node*,将其值设为先序第一个,记得将其左右子树设为nullptr!!!!!然后递归建造左右子树。

//根据先序遍历和中序(建树)来求后序遍历
#include<iostream>
#include<stack>
#include<vector>
#include<unordered_map>
using namespace std;
struct node{
    int val;
    node* lchild, *rchild;
};

vector<int> preOrder;
vector<int> inOrder;
vector<int> postOrder;
unordered_map<int, int> hashTable;
node* postOrderBuild(int preL, int preR, int inL, int inR){
    if(preL > preR) return nullptr;
    node* root = new node;
    root->val = preOrder[preL];
    root->lchild = nullptr;
    root->rchild = nullptr;
    int subLeft = hashTable[preOrder[preL]] - inL;
    root->lchild = postOrderBuild(preL + 1, preL + subLeft, inL,inL + subLeft);
    root->rchild = postOrderBuild(preL + subLeft + 1, preR, inL + subLeft + 1, inR);
    return root;
}

void postOrderTravese(node* root){
    if(root == nullptr) return;
    postOrderTravese(root->lchild);
    postOrderTravese(root->rchild);
    postOrder.push_back(root->val);
}

int main(){
    int n, num;
    cin>>n;
    stack<int> sta;
    string caozuo;
    while(cin>>caozuo){
        if(caozuo == "Push"){
            cin>>num;
            sta.push(num);
            preOrder.push_back(num);
        }
        else if(caozuo == "Pop"){
            num = sta.top();
            sta.pop();
            inOrder.push_back(num);
        }
    }
    //此时两个序列已经知道了,接下来是建树
    //还需要知道中序每个值的位置,方便了解它在第几个,知道左右子树的长度
    for(int i = 0; i < n; i++){
        hashTable[inOrder[i]] = i;//这样可以通过值找位置,比如这题hash[1]就等于3,这样就知道1在中序数组下标为3的位置
    }
    node* root = postOrderBuild(0,n-1,0,n-1);
    postOrderTravese(root);
    for(int i = 0; i < n; i++){
        if(i != 0) cout<<" ";
        cout<<postOrder[i];
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值