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 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 偷懒使用C++ C++ 真好用.
解题思路是根据IPO模型进行分解
根据输入我们能得到前序和中序,无非就是通过对字符的push 和pop的判断
将得到的前序和中序,存放到我们早已设置好的数组里
第二步 最重要的一步就是处理 如果用算法去根据前序和中序的结果去建立一颗
确定且唯一的二叉树
这里大力感谢两位博主的文章.写的非常棒,使用C++实现本体的博客
用代码实现前中序建树的博客
通过学习上面的博客,我搞懂了思想,在代码中大部分还是借鉴了第一篇博客,

代码
#include<stack>
#include<cstdio>
#include<cstring>
using namespace std;
typedef struct node{
        int data; // 节点值
        struct node* left;
        struct node* right;
}Node;
typedef Node*  Tree;
Tree BuildTree(int pre[],int in[],int psart,int pend, int istar, int iend) ;
void postTraversal(Tree root);
int N;
int cnt = 0;
int main()
{
      int x;
      int l1=0,l2=0; // 表示 pre in 数组的下标
      stack<int> st;
        scanf("%d", &N);
        int pre[N]; //初始化
        int in[N];  //初始化
        char ch[6];
        for( int i=0; i<2*N; i++)
        {
            scanf("%s",ch);
            if(strcmp(ch,"Push") == 0)
            {
                scanf("%d",&x);
                st.push(x);
                pre[l1++] = x;
            }else{
                in[l2++] = st.top();
                st.pop();
            }
        }
        Tree r = BuildTree(pre,in,0,N-1,0,N-1);
        postTraversal(r);
        return 0;
}

Tree BuildTree(int pre[],int in[],int psart,int pend, int istar, int iend) // 先序 中序数组,先序遍历的开头和结尾,中序遍历的开头和结尾
{
        Tree node = new Node; // 建立节点
        node->data = pre[psart]; //根据先序遍历的第一个节点建立根节点
        node->left = node->right = NULL; // 节点指向空
        if( psart == pend && istar == iend) return node; // 如果先序遍历只有一个 且开头就是结尾 那么他就是叶子结点
        int root = 0;
        for(root = istar; root<iend; root++) // 遍历给定的中序数组 找到pre[pstar] 在中序遍历的下标
        {
            if(pre[psart] == in[root]) 
            {
                break; // 如果找到就 让root==下标 退出循环
            }
        }
        int leftLength = root - istar ; // 中序遍历中的左子树的长度 等于 root-Istar
        int rightLength = iend - root; // 中序遍历中右子树长度 等于 右子树的末尾减去右子树根的下标
        // printf("leftlength = %d, rightlength = %d\n", leftLength, rightLength);
        if(leftLength > 0){ // 左子树还存在时候递归
        node->left = BuildTree(pre,in,psart+1, psart+leftLength, istar, root-1);
        }
        if(rightLength>0){ //右子树子树还存在时候递归
        node->right = BuildTree(pre, in,psart+leftLength+1, pend, root+1, iend );
        }
        return node;
}
void postTraversal(Tree root)
{
    if( root == NULL) 
    {
        return;
    }
    postTraversal(root->left);
    postTraversal(root->right);
    printf("%d", root->data);
    cnt++;
    if(cnt < N) printf(" ");
    
}




运行结果

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值