码迷,mamicode.com
首页 > 其他好文 > 详细

PAT006 Tree Traversals Again

时间:2015-04-12 16:06:42      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

题目:

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 == 0) {
            printf("%d", head->value);
            flag = 1;
        } else {
            printf(" %d", head->value);
        }
    }
}

int main()
{
    int nodeNum = 0;
    scanf("%d", &nodeNum);
    
    int operationCount = 2 * nodeNum;
    TraversalTreeNode *a[30];
    
    int top = -1;
    // 第一个节点肯定是PUSH
    int index = -1;
    scanf("%*s %d", &index);
    TraversalTreeNode *head = createTraversalTreeNode(index);
    a[0] = head;
    top = 0;
    
    TraversalTreeNode *popItem = NULL;
    for (int i = 1; i < operationCount; i++) {
        int index = -1;
        char str[4];
        scanf("%s", str);
        unsigned long len = strlen(str);
        if (len >= 4) {
            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 = 0;
    postorderTraversal(head);
}

运行结果:

技术分享

PAT006 Tree Traversals Again

标签:

原文地址:http://www.cnblogs.com/liufeng24/p/4419588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!