1020. Tree Traversals (25)

1020. Tree Traversals (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output:
4 1 6 3 5 7 2
解析:

1,根据后序遍历可以确定根结点,知道根结点后,根据中序遍历可以知道左右子树元素的分割线;

2,根据第一点,用递归的方法创建一棵树,然后用二叉树层次遍历的方法进行遍历.

#include <stdio.h>
#include <stdlib.h>

struct tree_node
{
    int data;
    struct tree_node *left;
    struct tree_node *right;
};


struct tree_node *buildTree(int *af, int afl, int afr, int *in, int inl, int inr)
{
    struct tree_node *ptr;
    int pos = 0;

    if (afl > afr)
        return NULL;
    else
    {
        ptr = (struct tree_node *)malloc(sizeof(struct tree_node));
        ptr->data = af[afr];
        pos = inl;
        while (*(in+pos) != ptr->data)
            pos++;
        ptr->left = buildTree(af, afl, afr-inr+pos-1, in, inl, pos-1);
        ptr->right = buildTree(af,afr-inr+pos, afr-1, in, pos+1, inr);
        return ptr;
    }
}

void printfLevelOrder(struct tree_node *root)
{
    struct tree_node qu[100];
    int front, rear;
    
    front = rear = -1;
    if (!root)
        return;
    rear++;
    qu[rear].data = root->data;
    qu[rear].left = root->left;
    qu[rear].right = root->right;
    
    while (rear != front)
    {
        if (front != -1)
            printf(" ");
        printf("%d", qu[++front].data);
        if (qu[front].left)
        {   rear++;
            qu[rear].data = qu[front].left->data;
            qu[rear].left = qu[front].left->left;
            qu[rear].right = qu[front].left->right;
            
        }
        if (qu[front].right)
        {   rear++;
            qu[rear].data = qu[front].right->data;
            qu[rear].left = qu[front].right->left;
            qu[rear].right = qu[front].right->right;
        }
    }
    printf("\n");
}

int main()
{
    int N;
    int af[128];
    int in[128];
    int i;
    struct tree_node *root;

    scanf("%d", &N);
    for (i = 0; i < N; i++)
        scanf("%d", af+i);
    for (i = 0; i < N; i++)
        scanf("%d", in+i);
    root = buildTree(af, 0, N-1, in, 0, N-1);
    printfLevelOrder(root);

    return 0;
}

代码说明:

第26,27行代码中

        ptr->left = buildTree(af, afl, afr-inr+pos-1, in, inl, pos-1);
        ptr->right = buildTree(af,afr-inr+pos, afr-1, in, pos+1, inr);

刚开始写成如下,总是报段错误:

        ptr->left = buildTree(af, afl, pos-1, in, inl, pos-1);
        ptr->right = buildTree(af,pos, afr-1, in, pos+1, inr);

index 0 1 2 3 4 5 6
af 2 3 1 5 7 6 4
in 1 2 3 4 5 6 7

观察5,7,6三个元素,就会发现第二种写法是错误的,改正的办法是怎样把上下两行5,7,6对不齐的情况考虑进去,因为pos根据in这个数字的下标得到的数子,所以pos应用到af数组时,把偏移量afr-inr=-1加上.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值