• Tree Traversals Again


    题目:Tree Traversals Again

    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 (≤) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2 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创建一个二叉树,(先序与中序),然后用后序输出。

    咋做:

    刚开始用的是结构体数组来做,我丢,调试了一整天一直都是那个测试点没过  ><  ,然后听别人说输入的节点的值数值可能相等,我丢..........

    后来改成了链式结构,然后有个小地方注意一下就A了。

    代码:

    #include <string>
    #include <map>
    #include <cmath>
    #include <limits>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <stack>
    #include <queue>
    #define Sci(x) scanf("%d",&x)
    #define Sci2(x, y) scanf("%d%d",&x,&y)
    #define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z)
    #define Scl(x) scanf("%I64d",&x)
    #define Scl2(x, y) scanf("%I64d%I64d",&x,&y)
    #define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z)
    #define Pri(x) printf("%d
    ",x)
    #define Prl(x) printf("%I64d
    ",x)
    #define For(i,x,y) for(int i=x;i<y;i++)
    #define FFor(i,x,y) for(int i=x;i>y;i--)
    #define For_(i,x,y) for(int i=x;i<=y;i++)
    #define FFor_(i,x,y) for(int i=x;i>=y;i--)
    #define Mem(f, x) memset(f,x,sizeof(f))
    #define LL long long
    #define ULL unsigned long long
    #define MAXSIZE 30
    #define INF 0x3f3f3f3f
    const int mod = 1e9+7;
    const double PI = acos(-1.0);
    
    using namespace std;
    typedef struct node
    {
        struct node *left;
        struct node *right;
        int data;
    } Tree,*Ptr;
    Ptr init(int a)
    {
        Ptr t;
        t=new Tree;
        t->left=t->right=NULL;
        t->data=a;
        return t;
    }
    int flag=1;
    void putt(Ptr t)
    {
        if(t!=NULL)
        {
            putt(t->left);
            putt(t->right);
            if(flag)
                printf("%d",t->data);
            else
                printf(" %d",t->data);
            flag=0;
        }
    }
    int main()
    {
        int n;
        Sci(n);
        Ptr T,p;
        T=new Tree;
        T->left=T->right=NULL;
        p=T;
        int a;
        stack<Ptr>s;
        char str[6];
        s.push(T);
        while(~scanf("%s",str))
        {
            if(strcmp(str,"Push")==0)
            {
                scanf("%d",&a);
                if(p->left==NULL)
                {
                    p->left=init(a);
                    s.push(p->left);
                    p=p->left;
                }
                else if(p->right==NULL)
                {
                    p->right=init(a);
                    s.push(p->right);
                    p=p->right;
                }
            }
            else
            {
                p=s.top();
                s.pop();
            }
        }
        putt(T->left);//此处传递的是根的左结点???上面的while循环里最开始入栈的是根的左结点,在题目输入时把这个结点当作根节点来使用,所以  T->left. QAQ
        return 0;
    }
    View Code
  • 相关阅读:
    条件、循环、函数定义 练习
    turtle库基础练习
    Python基础练习
    理解管理信息系统
    上拉刷新下拉加载的网站 mescroll
    数组的方法
    input只允许输入数字和小数
    selcet 苹果兼容
    base64码转图片的方式
    前端下载文件的方式
  • 原文地址:https://www.cnblogs.com/hbhdhd/p/11284790.html
Copyright © 2020-2023  润新知