二叉树的遍历C语言

二叉树作为FDS课程最核心的数据结构之一,要求每个人都掌握!

这是一道简单的二叉树问题!

我们将给出一颗二叉树,请你输出它的三种遍历,分别是先序遍历,中序遍历,后序遍历!

输入格式 :

二叉树将以这样的形式给出:

第一行给出一个正整数N(N<=100),表示二叉树上的节点个数!

接下来N行,每行包含三个整数,i,l,r,分别代表第i个节点的左右孩子!

如果它的左/右孩子为空,则在对应位置给出-1!

题目保证1是根节点!

输出格式 :

请你输出它的三种遍历!

第一行输出先序遍历,第二行输出中序遍历,第三行输出后序遍历!

每行末尾无多余空格!

输入样例 :

在这里给出一组输入。例如:

3

1 2 3

2 -1 -1

3 -1 -1

输出样例 :

在这里给出相应的输出。例如:

1 2 3

2 1 3

2 3 1

c 复制代码
#include <stdio.h>
#include <stdlib.h>

typedef int Elementyple;
typedef struct TNode* BiTree;
typedef struct TNode{
    Elementyple Data;
    struct TNode* Left;
    struct TNode* Right;
}tnode;

int N;

BiTree createNode() {
   BiTree node = new TNode;
    node->Left = NULL;
    node->Right = NULL;
    return node;
}

BiTree CreaTree(int number[10000][15],int x){
	if(x==-1)
	return NULL;
     BiTree BT;
    // BT=(BiTree)malloc(sizeof(struct TNode));
    BT=createNode();
     BT->Data=x;
     BT->Left=CreaTree(number,number[x][0]);
     BT->Right=CreaTree(number,number[x][1]);
     return BT;
}
int flag=0;

void PreorderTraversal( BiTree BT ){
    if(BT){
	    flag++;
	    if(flag==N)
	    printf("%d",BT->Data);
		else 
        printf("%d ",BT->Data);
        PreorderTraversal(BT->Left);
        PreorderTraversal(BT->Right);
    }
}
void InorderTraversal( BiTree BT){
    if(BT){
        InorderTraversal(BT->Left);
        flag++;
        if(flag==2*N)
            printf("%d",BT->Data);
        else
        printf("%d ",BT->Data);
        InorderTraversal(BT->Right);
        
    }
}
void PostorderTraversal( BiTree BT ){
    if(BT){
        PostorderTraversal(BT->Left);
        PostorderTraversal(BT->Right);
        flag++;
        if(flag==3*N)
        printf("%d",BT->Data);
        else
        printf("%d ",BT->Data);
    }
}
int main(){
    int i,a[10000][15],b[10000];
    scanf("%d",&N);
    for(i=1;i<=N;i++){
        scanf("%d%d%d",&b[i],&a[i][0],&a[i][1]);
    }
     BiTree BT; 
     BT=CreaTree(a,1);
     PreorderTraversal(BT);
     printf("\n");
     InorderTraversal(BT);
     printf("\n");
     PostorderTraversal(BT);
}
相关推荐
Hhy_11076 小时前
《C++深度解构07》手撕 vector —— 从底层三指针架构到迭代器失效彻底吃透
c语言·开发语言·c++·学习·visual studio
风落青芜6 小时前
链式二叉树的实现与基本操作
c语言·开发语言·数据结构·算法
风落青芜1 天前
数据结构:堆的概念与实现
c语言·开发语言·数据结构·算法
老花眼猫2 天前
C语言绘图方法:(10)椭圆缩放旋转
c语言·经验分享·青少年编程·课程设计
Zwawa3 天前
陀螺仪数据为什么看起来总在动:零偏、噪声和零漂
c语言·单片机·嵌入式
soso19683 天前
低功耗设计与BMS开发指南
c语言·开发语言·汇编
老师我太想进步了20263 天前
【数据结构-8】栈的实现
c语言·数据结构·算法
LCG元3 天前
串口乱码,先算波特率误差再动代码
c语言·嵌入式
another heaven4 天前
【C/C++ 第一次跨越(上):用 C++11/14 停止制造新的旧代码】
c语言·c++·制造
another heaven5 天前
【C/C++ Ranges 是零开销抽象吗?拆开管道、惰性求值和编译期膨胀】
c语言·开发语言·c++