题目如下:

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:

Push
Push
Push
Pop
Pop
Push
Pop
Pop
Push
Push
Pop
Pop

Sample Output:

     

分析:

  题意很简单,给你一个用栈遍历某棵树的顺序。并给出这个树的后序遍历。

  不妨看一下例子:Push进栈的有:1 2 3 4 5 6

          而对应Pop出栈的是:3 2 4 1 6 5

  发现很眼熟,如果对树的遍历很敏感的话。你会发现进栈的顺序是树的先序遍历。而出栈的顺序是中序遍历。

  暂且不讨论原因何在,就按这个思路,把树建起来,后序遍历输出即可。

  关键是,怎么建树?

  我的思路如下:按照题意,建立一个栈,两个数组pre,middle:用来保留先序遍历序列与中序遍历序列。

         处理Push和Pop的接受输入。当Push时,把对应数据计入先序遍历序列的对应下标。并把数据压栈。

                      当Pop是,把栈顶元素计入中序遍历序列的对应下标。并弹栈。

         这样输入结束后,我们就有了先序与中序序列。接下来的问题就变为:利用先序序列和中序序列还原二叉树,并后序输出。

         教材例题了,方法不多述了。关键步骤在注释中有提到。

代码如下:

#include <bits/stdc++.h>
#define MAX 101
typedef struct TNode
{
    int data;
    struct TNode *left;
    struct TNode *right;

}TNode;

};
};

int ps,pe,ms,me;

;
;

TNode* CreateTree(int ps,int pe,int ms,int me)
{
    int i;
    if(ps>pe)
        return NULL;

    //查找根节点在中序序列中位置
    for( i=ms;i<=me;i++)
    {
        if(middle[i]==pre[ps])
            break;
    }
    TNode *r = new TNode;

    //若第i个位置为根节点,则i-起始位置=左子树个数
    int num1=i-ms;
    r->data=pre[ps];
     //左子树对应:先序序列:根节点往后一个~根节点+左子树个数
     //                中序序列:起始位置(中序:左根右)~根节点位置(即i)
    r->left=CreateTree(ps+,ps+num1,ms,i-);
    //右子树对应:先序序列:左子树位置+1~ 列尾
     //                中序序列:根节点+1~列尾
    r->right=CreateTree(ps+num1+,pe,i+,me);
    return r;
}

 //后序遍历输出
void PostOrder(TNode *r)
{

    if(r==NULL)
        return;
    PostOrder(r->left);
    PostOrder(r->right);
    printf("%d",r->data);
    count++;
    if(count <total)
        printf(" ");

}

int main(void)
{
    scanf("%d",&total);
  std::stack<int> a;
    int tmp;
    ];
    ; //前序序列下标
    ; //中序序列下标
    ;i<*total;i++)
    {
        scanf("%s",tmps);
        )
        {
            scanf("%d",&tmp);
            pre[pn]=tmp;
            pn++;
            a.push(tmp);
        }
        else
        {
            middle[mn]=a.top();
            mn++;
            a.pop();
        }
    }
    TNode* root = CreateTree(, total-, , total-);
    PostOrder(root);
    ;

}

  

【PTA】Tree Traversals Again的更多相关文章

  1. 【POJ3237】Tree 树链剖分+线段树

    [POJ3237]Tree Description You are given a tree with N nodes. The tree's nodes are numbered 1 through ...

  2. 【BZOJ】【2631】Tree

    LCT 又一道名字叫做Tree的题目…… 看到删边加边什么的……又是动态树问题……果断再次搬出LCT. 这题比起上道[3282]tree的难点在于需要像线段树维护区间那样,进行树上路径的权值修改&am ...

  3. 【Luogu1501】Tree(Link-Cut Tree)

    [Luogu1501]Tree(Link-Cut Tree) 题面 洛谷 题解 \(LCT\)版子题 看到了顺手敲一下而已 注意一下,别乘爆了 #include<iostream> #in ...

  4. 【BZOJ3282】Tree (Link-Cut Tree)

    [BZOJ3282]Tree (Link-Cut Tree) 题面 BZOJ权限题呀,良心luogu上有 题解 Link-Cut Tree班子提 最近因为NOIP考炸了 学科也炸了 时间显然没有 以后 ...

  5. 【AtCoder3611】Tree MST(点分治,最小生成树)

    [AtCoder3611]Tree MST(点分治,最小生成树) 题面 AtCoder 洛谷 给定一棵\(n\)个节点的树,现有有一张完全图,两点\(x,y\)之间的边长为\(w[x]+w[y]+di ...

  6. 【HDU5909】Tree Cutting(FWT)

    [HDU5909]Tree Cutting(FWT) 题面 vjudge 题目大意: 给你一棵\(n\)个节点的树,每个节点都有一个小于\(m\)的权值 定义一棵子树的权值为所有节点的异或和,问权值为 ...

  7. 【BZOJ2654】Tree(凸优化,最小生成树)

    [BZOJ2654]Tree(凸优化,最小生成树) 题面 BZOJ 洛谷 题解 这道题目是之前\(Apio\)的时候写的,忽然发现自己忘记发博客了... 这个万一就是一个凸优化, 给所有白边二分一个额 ...

  8. 【POJ1741】Tree(点分治)

    [POJ1741]Tree(点分治) 题面 Vjudge 题目大意: 求树中距离小于\(K\)的点对的数量 题解 完全不觉得点分治了.. 简直\(GG\),更别说动态点分治了... 于是来复习一下. ...

  9. 点分治【bzoj1468】 Tree

    点分治[bzoj1468] Tree Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边 ...

随机推荐

  1. centos6 yum 安装 install c++4.8 gcc4.8

    cd /etc/yum.repos.d wget http://people.centos.org/tru/devtools-1.1/devtools-1.1.repo yum --enablerep ...

  2. 用命令关键字(Cmdlet Keyworlds)编写面向管道的脚本

    使用begin  process和end关键字 把你的脚本分成 初始化 处理和清楚几个区域

  3. 2018.8.17 关于JavaScript的几种常见的全局函数

    JavaScript常见的全局函数 <!doctype html> <html lang="en"> <head> <meta chars ...

  4. jdk8环境变量 jdk8图解安装 java8安装

    JDK8 是JDK的最新版本,加入了很多新特性,如果我们要使用,需要下载安装: JDK8在windows xp下安装有点问题,所以在WIN7下安装 WIN7操作系统有32位和64位,分别要下载对应的J ...

  5. leetCode没那么难啦 in Java (一)

    前言    感觉写博客是一个很耗心力的东西T_T,简单的写了似乎没什么用,复杂的三言两语也只能讲个大概,呸呸...怎么能有这些消极思想呢QAQ!那想来想去,先开一个leetcode的坑,虽然已经工作了 ...

  6. JavaEE权限管理系统的搭建(二)--------聚合工程项目的创建和依赖关系

    本项目是一个聚合工程,所以要先搭建一个聚合工程的框架 搭建完成的项目结构图如下: 首先创建父项目:pom类型 子模块:web层的搭建,war类型 把这个两个目录标记为对应的类型 其他子模块:和serv ...

  7. 动态生成的DOM做点击事件无效

    有时候我们的标签都是从后台获取的数据,然后利用JS添加到页面上,当我们写生成的标签的点击事件(click)时没有效果. 例如: <section> 测试动态生成的DOM点击事件 <b ...

  8. C# as运算符

    一.C# as运算符 as运算符用于执行引用类型的显式类型转换.请阅读C#数据类型. as运算符可以用下面的格式表示: expression as type?expression:引用类型的表达式. ...

  9. 2017年10月26日 git上传文件失败的文件

    最近几天因为项目要用git,于是学习了一下git.今天上传项目到码云的时候,却发现总有一些文件夹上传不上去,git 也显示everything is update.找了一圈办法,都没有用,最后突然发现 ...

  10. selenium学习总结

    selenium主要用来做web自动化,分1.0和2.0两个版本,1.0包括selenium IDE.selenium Grid.selenium Remote Control,2.0在1.0的基础上 ...