036二叉树的镜像(keep it up)

剑指offer中题目:http://ac.jobdu.com/problem.php?pid=1521

题目描述:

输入一个二叉树,输出其镜像。

036二叉树的镜像(keep it up)_第1张图片

输入:

输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行为一个整数n(0<=n<=1000,n代表将要输入的二叉树节点的个数(节点从1开始编号)。接下来一行有n个数字,代表第i个二叉树节点的元素的值。接下来有n行,每行有一个字母Ci。
Ci=’d’表示第i个节点有两子孩子,紧接着是左孩子编号和右孩子编号。
Ci=’l’表示第i个节点有一个左孩子,紧接着是左孩子的编号。
Ci=’r’表示第i个节点有一个右孩子,紧接着是右孩子的编号。
Ci=’z’表示第i个节点没有子孩子。

输出:

对应每个测试案例,
按照前序输出其孩子节点的元素值。
若为空输出NULL。

样例输入:
7
8 6 10 5 7 9 11
d 2 3
d 4 5
d 6 7
z
z
z
z
样例输出:
8 10 11 9 6 7 5
代码:

#include <stdio.h>
#include <stdlib.h>
 
#define MAXSIZE 1001
#define DEFAULTDATA 0x7fffffff
 
typedef struct STreeNode
{
    int data;
    int left;
    int right;
}STreeNode;
 
STreeNode* createNode(int vData)
{
    STreeNode *Node = (STreeNode*)malloc(sizeof(STreeNode));
    Node->data  = vData;
    Node->left  = -1;
    Node->right = -1;
    return Node;
}
 
void deleteNode(STreeNode **vNode)
{
    (*vNode)->left  = -1;
    (*vNode)->right = -1;
    free(*vNode);
    *vNode = NULL;
}
 
void allocNode(STreeNode *vNode[], int vSize)
{
    if (vSize <= 0 || vSize >= MAXSIZE) return;
 
    int i;
    for (i = 1; i <= vSize; ++i)
    {
        vNode[i] = createNode(DEFAULTDATA);
        if (vNode[i] == NULL) return;
    }
}
 
void deleteNode(STreeNode *vNode[], int vSize)
{
    if (vSize <= 0 || vSize >= MAXSIZE) return;
 
    int i;
    for (i = 1; i <= vSize; ++i)
    {
        deleteNode(&(vNode[i]));
    }
}
 
void clearNode(STreeNode *vNode[], int vSize)
{
    if (vSize <= 0 || vSize >= MAXSIZE) return;
 
    int i;
    for (i = 1; i <= vSize; ++i)
    {
        vNode[i]->data  = DEFAULTDATA;
        vNode[i]->left  = -1;
        vNode[i]->right = -1;
    }
}
 
void createBinaryTree(STreeNode *vTreeNode[], int vN)
{
    if (vN == 0) return;
 
    int  i;
    int  Left;
    int  Right;
    char Op;
 
    for (i = 1; i <= vN; ++i)
    {
        while (getchar() != '\n') continue;//这个地方坑爹,直接getchar()不正确
        scanf("%c", &Op);
 
        if (Op == 'd')
        {
            scanf("%d %d", &Left, &Right);
            vTreeNode[i]->left  = Left;
            vTreeNode[i]->right = Right;
        }
        else if (Op == 'l')
        {
            scanf("%d", &Left);
            vTreeNode[i]->left = Left;
        }
        else if (Op == 'r')
        {
            scanf("%d", &Right);
            vTreeNode[i]->right = Right;
        }
    }
}
 
void sweep(int *vLeft, int *vRight)
{
    int Data;
    Data    =*vLeft;
    *vLeft  =*vRight;
    *vRight = Data;
}
void switchTree(STreeNode *vTreeNode[], int vRoot)
{
    if (vRoot == -1) return;
    if (vTreeNode[vRoot]->left == -1 && vTreeNode[vRoot]->right == -1) return;
 
    sweep(&(vTreeNode[vRoot]->left), &(vTreeNode[vRoot]->right));
    switchTree(vTreeNode, vTreeNode[vRoot]->left); 
    switchTree(vTreeNode, vTreeNode[vRoot]->right);
}
 
void prePrintTree(STreeNode *vTreeNode[], int vRoot, int &vCount, int vN)
{
    if (vRoot == -1) return;
 
    if (vCount == vN) printf("%d\n", vTreeNode[vRoot]->data);
    else printf("%d ", vTreeNode[vRoot]->data);
 
    ++vCount;
    prePrintTree(vTreeNode, vTreeNode[vRoot]->left, vCount, vN); 
    prePrintTree(vTreeNode, vTreeNode[vRoot]->right, vCount, vN);
}
 
 
int main()
{
    int i;
    int N;
    STreeNode *TreeNode[MAXSIZE];
 
    allocNode(TreeNode, MAXSIZE-1);
 
    while (scanf("%d", &N) != EOF)
    {
        if (N == 0)
        {
            printf("NULL\n");
            continue;
        }
 
        for (i=1; i <= N; ++i)
        {
            scanf("%d", &(TreeNode[i]->data));
        }
 
        createBinaryTree(TreeNode, N);
        switchTree(TreeNode, 1);
        int Count = 1;
        prePrintTree(TreeNode, 1, Count, N);
        clearNode(TreeNode, N);
    }
 
    deleteNode(TreeNode, MAXSIZE-1);
 
    return 0;
}
 
/**************************************************************
    Problem: 1521
    User:
    Language: C++
    Result: Accepted
    Time:0 ms
    Memory:1020 kb
****************************************************************/


你可能感兴趣的:(Algorithm,C++,算法,二叉树)