PAT006 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 (<=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
分析: 主要是根据输入创建一个二叉树,然后进行后续遍历
代码:
#pragma mark -Tree Traversals Again
#include <stdio.h> typedef struct traversalTreeNode {
int value;
struct traversalTreeNode *left;
struct traversalTreeNode *right;
} TraversalTreeNode; int flag; TraversalTreeNode *createTraversalTreeNode(int value)
{
TraversalTreeNode *node = (TraversalTreeNode *)malloc(sizeof(TraversalTreeNode));
node->value = value;
node->left = NULL;
node->right = NULL;
return node;
} void postorderTraversal(TraversalTreeNode *head)
{
if (head) {
postorderTraversal(head->left);
postorderTraversal(head->right); if (flag == ) {
printf("%d", head->value);
flag = ;
} else {
printf(" %d", head->value);
}
}
} int main()
{
int nodeNum = ;
scanf("%d", &nodeNum); int operationCount = * nodeNum;
TraversalTreeNode *a[]; int top = -;
// 第一个节点肯定是PUSH
int index = -;
scanf("%*s %d", &index);
TraversalTreeNode *head = createTraversalTreeNode(index);
a[] = head;
top = ; TraversalTreeNode *popItem = NULL;
for (int i = ; i < operationCount; i++) {
int index = -;
char str[];
scanf("%s", str);
unsigned long len = strlen(str);
if (len >= ) {
scanf("%d", &index);
TraversalTreeNode *newNode = createTraversalTreeNode(index);
if (popItem) {
if (!popItem->left) {
popItem->left = newNode;
} else {
popItem->right = newNode;
}
} else {
if (!a[top]->left) {
a[top]->left = newNode;
} else {
a[top]->right = newNode;
}
} top++;
a[top] = newNode;
popItem = NULL;
} else {
popItem = a[top];
a[top] = NULL;
top--;
}
} flag = ;
postorderTraversal(head);
}
运行结果:
PAT006 Tree Traversals Again的更多相关文章
- Tree Traversals
Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU1710Binary Tree Traversals
HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...
- HDU-1701 Binary Tree Traversals
http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- HDU 1710-Binary Tree Traversals(二进制重建)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- PAT1086:Tree Traversals Again
1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- Binary Tree Traversals(HDU1710)二叉树的简单应用
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- HDUOJ A Mathematical Curiosity 1017
此题不难就是输出格式麻烦 #include<stdio.h> int main(){ int T; scanf("%d",&T); ...
- OC中数组的使用方法
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { // 创建数组 NS ...
- 前端性能优化:DocumentFragments或innerHTML取代复杂的元素注入
来源:GBin1.com 我们的浏览器执行越来越多的特性,并且网络逐渐向移动设备转移,使我们的前端代码更加紧凑,如何优化,就变得越来越重要了.前端给力的地方是可以有 许多种简单的策略和代码习惯让我们可 ...
- MongoDB数据库设计中6条重要的经验法则
Part 1 原文:6 Rules of Thumb for MongoDB Schema Design: Part 1 By William Zola, Lead Technical Support ...
- RMQ 算法入门
1. 概述 RMQ(Range Minimum/Maximum Query).即区间最值查询,是指这样一个问题:对于长度为n的数列A,回答若干询问RMQ(A,i,j)(i,j<=n),返回数列A ...
- 一个用于将sql脚本转换成实体类的js代码
以前写过一段C#,苦于编译才能用.这样的小工具最好是用脚本语言来编写,易于执行,也易于修改. js 代码 convert.js ------------------------------------ ...
- (C#)Windows Shell 外壳编程系列1 - 基础,浏览一个文件夹
1 - 基础,浏览一个文件夹 我们知道,在win32中是以外壳名字空间的形式来组织文件系统的,在外壳名字空间里的每一个对象(注)都实现了一个IShellFolder的接口,通过这个接口我们可以直接查询 ...
- Linux下Tomcat 8080 端口被占用的解决办法
希望可以帮助你们 一,停止tomcat 并执行#netstat -an|grep 8080 查看发现有许多80端口进程在里面 二,执行# lsof -i :8080|grep -v "P ...
- python oop面向对象笔记
#coding:utf-8 class Person(object): def __init__(self,name,wage): self.name = name self.wage = wage ...
- 集成讯飞听写iOS sdk到unity遇到的问题:weak成员和strong成员
在unity里集成讯飞语音听写iOS sdk的过程中,遇到一个问题,官方的demo中可以将多次onResults回调返回的结果累积拼接起来组成一个完整的结果,而我集成过来以后就不能累积了,只拿到最后一 ...