【c语言数据结构】遍历二叉树

``````#include<stdio.h>
#include<stdlib.h>
typedef struct treeNode//定义
{
int data;
struct treeNode *left;
struct treeNode *right;
}treenode,*TreeNode;
void pre(TreeNode node)//前序遍历
{

if(node==NULL)
return ;
printf("%d ", node->data);
pre(node->left);
pre(node->right);
}
void mid(TreeNode node)//中序遍历
{

if(node==NULL)
return ;
mid(node->left);
printf("%d ", node->data);
mid(node->right);
}
void beh(TreeNode node)//后序遍历
{

if(node==NULL)
return ;
beh(node->left);
beh(node->right);
printf("%d ", node->data);
}
void tree(TreeNode one)//定义一个现成的二叉树
{
one=(TreeNode*)malloc(sizeof(treenode));
one->left=(TreeNode*)malloc(sizeof(treenode));
one->right=(TreeNode*)malloc(sizeof(treenode));
one->right->left=(TreeNode*)malloc(sizeof(treenode));
one->right->right=(TreeNode*)malloc(sizeof(treenode));
one->left->left=(TreeNode*)malloc(sizeof(treenode));
one->left->right=(TreeNode*)malloc(sizeof(treenode));
one->right->left->left=(TreeNode*)malloc(sizeof(treenode));
one->right->left->right=(TreeNode*)malloc(sizeof(treenode));
one->right->right->left=(TreeNode*)malloc(sizeof(treenode));
one->right->right->right=(TreeNode*)malloc(sizeof(treenode));

one->data=3;
one->left->data=9;
one->right->data=20;
one->right->left->data=15;
one->right->right->data=7;
}
void main()//主方法
{
TreeNode  one;
tree(one);
printf("该二叉树的前序遍历为:\n");
pre(one);
printf("该二叉树的中序遍历为:\n");
mid(one);
printf("该二叉树的后序遍历为:\n");
beh(one);
}

``````
• 写回答

1条回答默认 最新

• threenewbee 2020-02-13 21:10
关注
``````// Q1054302.cpp : Defines the entry point for the console application.
//

#include<stdio.h>
#include<stdlib.h>
typedef struct treeNode//定义
{
int data;
treeNode *left;
treeNode *right;
} treenode, *TreeNode;
void pre(TreeNode node)//前序遍历
{

if(node==NULL)
return ;
printf("%d ", node->data);
pre(node->left);
pre(node->right);
}
void mid(TreeNode node)//中序遍历
{

if(node==NULL)
return ;
mid(node->left);
printf("%d ", node->data);
mid(node->right);
}
void beh(TreeNode node)//后序遍历
{

if(node==NULL)
return ;
beh(node->left);
beh(node->right);
printf("%d ", node->data);
}
void tree(TreeNode one)//定义一个现成的二叉树
{
one->data=3;
one->left=(treenode*)malloc(sizeof(treenode));
one->left->data=9;
one->left->left=NULL;
one->left->right=NULL;
one->right=(treenode*)malloc(sizeof(treenode));
one->right->data=20;
one->right->left=(treenode*)malloc(sizeof(treenode));
one->right->left->data=15;
one->right->left->left=NULL;
one->right->left->right=NULL;
one->right->right=(treenode*)malloc(sizeof(treenode));
one->right->right->data=7;
one->right->right->left=NULL;
one->right->right->right=NULL;
}
int main()//主方法
{
TreeNode  one;
one=(treenode*)malloc(sizeof(treenode));
tree(one);
printf("该二叉树的前序遍历为:\n");
pre(one);
printf("\n该二叉树的中序遍历为:\n");
mid(one);
printf("\n该二叉树的后序遍历为:\n");
beh(one);
}

``````

该二叉树的前序遍历为:
3 9 20 15 7
该二叉树的中序遍历为:
9 3 15 20 7
该二叉树的后序遍历为:
9 15 7 20 3 Press any key to continue . . .

本回答被题主选为最佳回答 , 对您是否有帮助呢?
评论

悬赏问题

• ¥15 安装powerbuilder10卡在安装程序正在运行这个页面 没有下一步任何指令
• ¥15 关于mpi的问题：请问遇到这种情况需要怎么解决，出现这个问题后电脑不能进行mpi多核运行只能进行单核运行
• ¥50 微信聊天记录备份到电脑提示成功了，但还是没同步到电脑微信
• ¥15 python怎么在已有视频文件后添加新帧
• ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
• ¥15 fluent里模拟降膜反应的UDF编写