03-树3. Tree Traversals Again (25)

题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913

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

题目实质是通过先序遍历和中序遍历建树,再后序遍历树。
解题思路
1. 通过输入建树
    Push操作代表新建一个节点,将其与父节点连接并同时压栈
    Pop操作,从栈顶弹出一个节点
2. 后序遍历:递归实现
代码如下:
#include <cstdio>
#include <cstring>
#include <cstdlib> #define STR_LEN 5
#define MAX_SIZE 30 typedef struct Node
{
int data;
struct Node *left, *right;
}* treeNode; treeNode Stack[MAX_SIZE];
int values[MAX_SIZE]; int num = ;
int top = -; void Push(treeNode tn);
treeNode Pop();
treeNode Top();
bool isEmpty(); void PostOrderTraversal(treeNode root); int main()
{
int n;
char operation[STR_LEN];
treeNode father, root;
bool findRoot = , Poped = ; scanf("%d", &n);
for (int i = ; i < * n; i++)
{
scanf("%s", operation);
if (strcmp(operation, "Push") == )
{
int value;
scanf("%d", &value);
treeNode newNode;
newNode = (treeNode)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
if (!findRoot)
{
root = newNode; //根节点
Push(newNode);
findRoot = ;
}
else
{
if (!Poped) //如果前一个操作不是pop,则父节点为栈顶元素
father = Top();
if (father->left == NULL)
father->left = newNode;
else
father->right = newNode;
//printf("%d\n", newNode->data);
Push(newNode);
}
Poped = ;
}
else
{
father = Pop();
Poped = ;
}
}
PostOrderTraversal(root); for (int i = ; i < num-; i++)
printf("%d ", values[i]);
printf("%d\n", values[num-]); return ;
} void PostOrderTraversal(treeNode root)
{
treeNode tn = root;
if(tn)
{
PostOrderTraversal(tn->left);
PostOrderTraversal(tn->right);
values[num++] = tn->data; //将后序遍历出的节点值存入数组便于格式化打印
}
} void Push(treeNode tn)
{
Stack[++top] = tn;
} treeNode Pop()
{
return Stack[top--];
} bool isEmpty()
{
return top == -;
} treeNode Top()
{
return Stack[top];
}



03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历的更多相关文章

  1. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  2. pat03-树3. Tree Traversals Again (25)

    03-树3. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. pat1086. Tree Traversals Again (25)

    1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  4. PTA 03-树3 Tree Traversals Again (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again   (25分) An inor ...

  5. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  6. 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)

    7-5 Tree Traversals Again (25 分)   An inorder binary tree traversal can be implemented in a non-recu ...

  7. PAT 1043 Is It a Binary Search Tree (25分) 由前序遍历得到二叉搜索树的后序遍历

    题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...

  8. PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]

    题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...

  9. 1086. Tree Traversals Again (25)

    题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...

随机推荐

  1. asp.net 设计条码code 11的问题

    前一段时间思考了一些条码生成的问题,其实条码也可以说是加密的文件显示. 一个条码首先要有规定 比如code 11 又 1234567890 - 这11个字符组成 而1 又用 5码 表示 "1 ...

  2. Dubbo背景和简介

    转载出处 Dubbo开始于电商系统,因此在这里先从电商系统的演变讲起. 单一应用框架(ORM) 当网站流量很小时,只需一个应用,将所有功能如下单支付等都部署在一起,以减少部署节点和成本. 缺点:单一的 ...

  3. Facebook190亿美元收购WhatsApp

    Facebook收购WhatsApp,前后只花费10天时间.这是Facebook迄今规模最大的一笔收购,可能也是史上最昂贵的一笔针对靠私人风投起家的企业的收购案. 2月9日,马克•扎克伯格(Mark ...

  4. ShipStation Now Uses AWS And Amazon Fulfillment To Automatically Ship From eBay, Sears And Other Marketplaces

    ShipStation today unveiled a first-of-its-kind service to leverage Amazon Web Services and Amazon.co ...

  5. AngularJS学习之数据绑定

    既然AngularJS是以数据作为驱动的MVC框架,在上一篇文章中,也介绍了AngularJS如何实现MVC模式的,所有模型里面的数据,都必须经过控制器,才能展示到视图中. 什么是数据绑定 首先来回忆 ...

  6. C++:构造函数1——普通构造函数

    前言:构造函数是C+中很重要的一个概念,这里对其知识进行一个简单的总结 一.构造函数的定义 1.类中的构造函数名与类名必须相同 2.构造函数没有函数的返回类值型说明符 [特别注意]: a.构造函数的返 ...

  7. 线段树---poj2528 Mayor’s posters【成段替换|离散化】

    poj2528 Mayor's posters 题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报 思路:这题数据范围很大,直接搞超时+超内存,需要离散化: 离散化简单的来说就是只取我们需要 ...

  8. C#高级编程(第六版)学习:第三十一章:Windows窗体

    第三十一章 Windows窗体 创建Windows窗体应用程序 在文本编辑器中输入: /* * form.cs * a simple windows form * */ using System; u ...

  9. 第6题 ZigZag转换

    题目描述如下: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ro ...

  10. HTML&CSS实体

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...