ACM题目————二叉树的遍历

一、二叉树的后序遍历:

题目描述

给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列。本题假设二叉树的结点数不超过1000

输入

输 入数据分为多组,第一行是测试数据的组数n,下面的n行分别代表一棵二叉树。每棵二叉树的结点均为正整数,数据为0代表当前结点为空,数据为-1代表二叉 树数据输入结束,-1不作处理。二叉树的构造按照层次顺序(即第1层1个整数,第2层2个,第3层4个,第4层有8个......,如果某个结点不存在以 0代替)。

输出

输出每棵二叉树的深度以及后序遍历二叉树得到的序列。

样例输入
2
1 -1
1 2 0 3 4 -1
样例输出

1 1

3 3 4 2 1

//Asimple
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;const int maxn = 1005;
int n, T, num, cnt, point, line, x, y, t;
bool flag;
 
typedef struct node
{
    int data ;
    struct node *lchild, *rchild;
}BiNode, *BiTree;
 
BiTree *q[maxn];
 
int Deepth(BiTree T)
{
    if( T == NULL ) return 0 ;
    int x = Deepth(T->lchild);
    int y = Deepth(T->rchild);
    return max(x,y)+1 ;
}
 
void Hou_Print(BiTree T)
{
    if( T == NULL ) return ;
    Hou_Print(T->lchild);
    Hou_Print(T->rchild);
    cout << " " << T->data ;
}
 
int main()
{
    BiTree u, v, root;
    int f, r;
    cin >> T ;
    while( T -- )
    {
        flag = true ;
        f = r = 0 ;
        while( scanf("%d",&num)&&num!=-1)//建树
        {
            if( flag )//头节点
            {
                root = (BiTree)malloc(sizeof(BiNode));
                root->data = num ;
                root->lchild = root->rchild = NULL ;
                if( root->data == 0 )
                {
                    root = NULL ;
                    cout << "0 0" << endl ;
                    break;
                }
                q[r++] = &root->lchild;
                q[r++] = &root->rchild;
                flag = false ;
            }
            else
            {
                u = (BiTree)malloc(sizeof(BiNode));
                u->data = num ;
                u->lchild = u->rchild = NULL ;
                if( u->data != 0 )
                {
                    q[r++] = &u->lchild;
                    q[r++] = &u->rchild;
                }
                else u = NULL ;
                *q[f++] = u ;
            }
        }
        cnt = Deepth(root);
        cout << cnt ;
        Hou_Print(root);
        cout << endl ;
    }
 
    return 0;
}

二、中序遍历二叉树

题目描述

给定一颗二叉树,要求输出二叉树的深度以及中序遍历二叉树得到的序列。本题假设二叉树的结点数不超过1000。

输入

输 入数据分为多组,第一行是测试数据的组数n,下面的n行分别代表一棵二叉树。每棵二叉树的结点均为正整数,数据为0代表当前结点为空,数据为-1代表二叉 树数据输入结束,-1不作处理。二叉树的构造按照层次顺序(即第1层1个整数,第2层2个,第3层4个,第4层有8个......,如果某个结点不存在以 0代替)

输出

输出每棵二叉树的深度以及中序遍历二叉树得到的序列。

样例输入
2
1 -1
1 2 0 3 4 -1
样例输出

1 1

3 3 2 4 1

//Asimple
#include <stdio.h>
#include <iostream>
 
using namespace std;const int maxn = 1005;
int n, T, num, cnt, point, line, x, y, t;
bool flag;
 
typedef struct node
{
    int data ;
    struct node *lchild, *rchild;
}BiNode, *BiTree;
 
BiTree *q[maxn];
 
int Deepth(BiTree T)
{
    if( T == NULL ) return 0 ;
    int x = Deepth(T->lchild);
    int y = Deepth(T->rchild);
    return max(x,y)+1 ;
}
 
void Zhong_Print(BiTree T)
{
    if( T == NULL ) return ;
    Zhong_Print(T->lchild);
    cout << " " << T->data ;
    Zhong_Print(T->rchild);
}
 
int main()
{
    BiTree u, v, root;
    int f, r;
    cin >> T ;
    while( T -- )
    {
        flag = true ;
        f = r = 0 ;
        while( scanf("%d",&num)&&num!=-1)//建树
        {
            if( flag )//头节点
            {
                root = (BiTree)malloc(sizeof(BiNode));
                root->data = num ;
                root->lchild = root->rchild = NULL ;
                if( root->data == 0 )
                {
                    root = NULL ;
                    cout << "0 0" << endl ;
                    break;
                }
                q[r++] = &root->lchild;
                q[r++] = &root->rchild;
                flag = false ;
            }
            else
            {
                u = (BiTree)malloc(sizeof(BiNode));
                u->data = num ;
                u->lchild = u->rchild = NULL ;
                if( u->data != 0 )
                {
                    q[r++] = &u->lchild;
                    q[r++] = &u->rchild;
                }
                else u = NULL ;
                *q[f++] = u ;
            }
        }
        cnt = Deepth(root);
        cout << cnt ;
        Zhong_Print(root);
        cout << endl ;
    }
 
    return 0;
}

三、前序遍历:

题目描述

给定一颗二叉树,要求输出二叉树的深度以及先序遍历二叉树得到的序列。本题假设二叉树的结点数不超过1000。

输入

输入数据分为多组,第一行是测试数据的组数n,下面的n行分别代表一棵二叉树。每棵二叉树的结点均为正整数,数据为0代表当前结点为空,数据为-1 代表二叉树数据输入结束,-1不作处理。二叉树的构造按照层次顺序(即第1层1个整数,第2层2个,第3层4个,第4层有8个......,如果某个结点 不存在以0代替),

输出

输出每棵二叉树的深度以及先序遍历二叉树得到的序列。

样例输入
2
1 -1
1 2 0 3 4 -1
样例输出

1 1

3 1 2 3 4

//Asimple
#include <stdio.h>
#include <iostream>

using namespace std;const int maxn = 1005;
int n, T, num, cnt, point, line, x, y, t;
bool flag;

typedef struct node
{
    int data ;
    struct node *lchild, *rchild;
}BiNode, *BiTree;

BiTree *q[maxn];

int Deepth(BiTree T)
{
    if( T == NULL ) return 0 ;
    int x = Deepth(T->lchild);
    int y = Deepth(T->rchild);
    return max(x,y)+1 ;
}

void Qian_Print(BiTree T)
{
    if( T == NULL ) return ;
    cout << " " << T->data ;
    Qian_Print(T->lchild);
    Qian_Print(T->rchild);
}

int main()
{
    BiTree u, v, root;
    int f, r;
    cin >> T ;
    while( T -- )
    {
        flag = true ;
        f = r = 0 ;
        while( scanf("%d",&num)&&num!=-1)//建树
        {
            if( flag )//头节点
            {
                root = (BiTree)malloc(sizeof(BiNode));
                root->data = num ;
                root->lchild = root->rchild = NULL ;
                if( root->data == 0 )
                {
                    root = NULL ;
                    cout << "0 0" << endl ;
                    break;
                }
                q[r++] = &root->lchild;
                q[r++] = &root->rchild;
                flag = false ;
            }
            else
            {
                u = (BiTree)malloc(sizeof(BiNode));
                u->data = num ;
                u->lchild = u->rchild = NULL ;
                if( u->data != 0 )
                {
                    q[r++] = &u->lchild;
                    q[r++] = &u->rchild;
                }
                else u = NULL ;
                *q[f++] = u ;
            }
        }
        cnt = Deepth(root);
        cout << cnt ;
        Qian_Print(root);
        cout << endl ;
    }

    return 0;
}

树。。