1086 Tree Traversals Again (25分)

news/2024/9/23 9:37:22/

1 题目

1086 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.
Figure1
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
PopSample Output:
3 4 2 6 5 1

2 解析

  • 题意:根据栈的出栈、入栈序列,输出二叉树的后序序列
  • 思路:
    • 入栈序列为先序序列,出栈序列为中序序列
    • 1 根据先序和中序序列建立二叉树,然后后序遍历输出二叉树

3 参考代码

#include <cstdio>
#include <iostream>
#include <stack>
#include <string>using std::cin;
using std::stack;
using std::string;const int MAXN = 35;
int in[MAXN];
int pre[MAXN];
int N;struct node
{int data;node* lchild;node* rchild;
};node* Create(int preL, int preR, int inL, int inR){if(preL > preR){return NULL;}node* root = new node;root->data = pre[preL];int k;for (k = inL; k <= inR; ++k){if(in[k] == root->data){break;}}int numLeft = k - inL;root->lchild = Create(preL + 1, preL + numLeft, inL, k - 1);root->rchild = Create(preL + numLeft + 1, preR, k + 1, inR);return root;
}int num = 0;
void postorder(node* root){if(root == NULL){return;}postorder(root->lchild);postorder(root->rchild);printf("%d", root->data);num++;if(num < N) printf(" ");
}int main(int argc, char const *argv[])
{scanf("%d", &N);string str;int temp, num,inCount = 0,preCount = 0;stack<int> s;for (int i = 0; i < 2 * N; ++i){cin >> str;if(str == "Push"){scanf("%d", &num);s.push(num);pre[preCount++] = num;}else{temp = s.top();s.pop();in[inCount++] = temp;}}node* root = Create(0, N - 1, 0, N - 1);postorder(root);return 0;
}

http://www.ppmy.cn/news/289324.html

相关文章

HDU 6030(矩阵快速幂+规律)

传送门 题目描述&#xff1a; Happy Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1146 Accepted Submission(s): 491 Problem Description Little Q wants to buy a necklace for his girlfriend…

IntelliJ IDEA pycharm webstorm 激活

前端&#xff0c;后端&#xff0c;爬虫全套激活&#xff1a; IntelliJ IDEA 2017.2.5 破解 激活 http://xidea.online/?/download/intellij-idea 最新的pycharm 破解 激活 http://www.imsxm.com/jetbrains-license-server.html http://www.imsxm.com 最新的webstorm破解 激活 …

开博寄语

在这个特殊的日子开通了博客&#xff0c;今天是阴历的中元节&#xff0c;又是我的生日&#xff0c;已届不惑&#xff0c;混迹IT20多年&#xff0c;欢喜过、失落过、迷惘过&#xff0c;在这里开一片小小的天地&#xff0c;记录自己的心路历程&#xff0c;与有缘人共勉&#xff0…

1068 Find More Coins (30分)

文章目录 1 题目2 解析2.1 题意2.2 思路 3 参考代码 1 题目 1068 Find More Coins (30分) Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds o…

听音室-HIFI入门之400多张发烧碟中选出的精品

折腾这些年&#xff0c;也算对这个Hi-Fi,即所谓的音响发烧有些认识&#xff0c;这里大谈得比较多的是音响器材&#xff0c;Hi-end器材动辄几十万&#xff0c;有的上百万&#xff0c;为什么我们花这么多精力和金钱的投入&#xff1f;是因为高保真的音乐才是我们追求的本质&#…

pcm5102a解码芯片音质评测_WHAT HIFI推荐2020年最值得购买解码器:11款器材上榜

下面是《What Hi-Fi》推荐的2020年最值得购买解码器,来看看有哪些上榜器材吧! 一、和弦Chord Qutest便携式解码器 查看 和弦Chord Qutest便携式解码器 评测>> 目前该产品有“HIFI说影音城”商家在售,点击查看>> 口碑评说: “听完了上述曲目,我可以明显感受到Q…

[zz] 高端HIFI发烧音频DAC解码芯片排名

音频“解码器”中最核心、重要的器件&#xff0c;无非就是“解码”&#xff08;DAC&#xff0c;数模转换&#xff09;芯片了&#xff0c;大家常常很关注音频DAC芯片的选用&#xff0c;也热衷于对其优劣的讨论。 本文尝试对当前最优秀的高端音频DAC芯片的结构、技术和性能等做简…

真无线蓝牙耳机也能享受HIFI音质,2021五大高人气TWS蓝牙耳机推荐

当代生活&#xff0c;智能手机的耳机孔已经变成了奢侈品。而相对的&#xff0c;有线耳机的市场也被无线耳机不断蚕食。 22 日&#xff0c;光大证券发布了一个有关于目前真无线耳机的行业调查报告。从有一根线连着的颈戴式耳机&#xff0c;再到现在主流的真无线式耳机。这个行业…