03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历

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 = 0;
int top = -1;

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 = 0, Poped = 0;

    scanf("%d", &n);
    for (int i = 0; i < 2 * n; i++)
    {
        scanf("%s", operation);
        if (strcmp(operation, "Push") == 0)
        {
            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 = 1;
            }
            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 = 0;
        }
        else
        {
            father = Pop();
            Poped = 1;
        }
    }
    PostOrderTraversal(root);

    for (int i = 0; i < num-1; i++)
        printf("%d ", values[i]);
    printf("%d\n", values[num-1]);

    return 0;
}

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 == -1;
}

treeNode Top()
{
    return Stack[top];
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏高性能服务器开发

(五)sparkline微线图

sparkline这个单词,我第一次看的时候,也不知道这什么意思啊,以前根本没听过啊,但是这真真实实的出现在了redis的代码中了,刚刚开始以为这也是属于普通...

398120
来自专栏技术栈大杂烩

Python的Sequence切片下标问题

在python中, 切片是一个经常会使用到的语法, 不管是元组, 列表还是字符串, 一般语法就是:

14020
来自专栏码匠的流水账

聊聊storm的IEventLogger

storm-2.0.0/storm-client/src/jvm/org/apache/storm/metric/IEventLogger.java

9520
来自专栏鸿的学习笔记

Python写的Python解释器(五)

条件和循环 到目前为止,解释器仅仅只是简单的逐个执行指令。下面将会讲述需要多次执行某些指令,或者在特定条件下跳过它们的做法。在代码中编写循环和if语句时,解释器...

12820
来自专栏五毛程序员

五毛的cocos2d-x学习笔记05-场景与场景动画,动作

19660
来自专栏杨熹的专栏

【LEETCODE】模拟面试-84-Largest Rectangle in Histogram

题目: https://leetcode.com/problems/largest-rectangle-in-histogram/ Given n non-n...

36350
来自专栏转载gongluck的CSDN博客

cocos2dx 连连看

#include "GameLink.h" #include "CountDownBar.h" USING_NS_CC; Scene* GameLink::...

34150
来自专栏游戏杂谈

勿乱动arguments对象

此题是看到51js论坛上有人提出这个问题:求解arguments对象的内部实现原理

11220
来自专栏landv

vb.net_一个半成品

20440
来自专栏码匠的流水账

聊聊rocketmq的ProducerImpl

io/openmessaging/rocketmq/producer/ProducerImpl.java

11410

扫码关注云+社区

领取腾讯云代金券