文章

2

粉丝

0

获赞

11

访问

268

头像
判断二叉树是否对称 题解:
P1551 东北大学机试题
发布于2026年3月25日 17:20
阅读数 125

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//层次遍历的方式输入一个二叉树,
//判断这个二叉树的结构(即不用管结点的值)是否镜像对称。
typedef struct node
{
    char data;
    struct node *lchild,*rchild;
} BitNode,*BitTree;
//层序遍历创建二叉树,传入字符数组和数组大小
BitTree createTree(char *s,int size)
{
    if(size==0||s[0]=='#') return NULL;
    //创建根结点
    BitTree root=malloc(sizeof(BitNode));
    root->data=s[0];
    root->lchild=root->rchild=NULL;
    //创建一个数组模拟队列,根节点入队
    BitTree* queue=(BitTree*)malloc(sizeof(BitTree)*size);
    int front=0,rear=0;
    queue[rear++]=root;

    int index=1;
    //输入一行字母,其中#表示空节点(字母长度小于1000)。
    while(front<rear&&index<size)
    {
        //出队
        BitTree cur=queue[front++];
        //左孩子
  &nbs...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发