一只特立独行的青蛙 2018-09-09 10:59 采纳率: 50%
浏览 2340
已采纳

数据结构求二叉树的宽度问题

小白刚学..可能错误有点多,求大神解答

错误提示:
error C2146: syntax error : missing ';' before identifier 'A'
error C2501: 'St' : missing storage-class or type specifiers
fatal error C1004: unexpected end of file found

求二叉树宽度.exe - 1 error(s), 0 warning(s)

#include
#include
#define maxsize 20

St A[maxsize];

int L=1;//记录层数
int i=0;
void width(BTNode *p);
BTNode *CreateBiTree();

void main()
{
int j,k=0;
int max=0;
BiTree T;
T = CreateBiTree();//建立
width(T);

    for(j=0;j<=i;j++)
    {
      if(A[j].level==A[j+1].level)
      {
        ++k;
            max=(k>max?k:max);
      }
      else
              k=1;
    }
    printf("%d\n",max);

}

typedef struct BTNode{
int data;
struct BTNode *lchild,*rchild;
}BTNode;

//先序建立二叉树
BTNode *CreateBiTree(){
char ch;
BTNode *T;
scanf("%c",&ch);
if(ch=='#')T=NULL;
else{
T = (BTNode *)malloc(sizeof(BTNode));
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T;//返回根节点
}

typedef struct
{
BTNode *q;
int level;
}St;

void width(BTNode *p)
{

A[i].q=p;
A[i].level=L;
++i;
++L;
width(p->lchild);
width(p->rchild);
--L;

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2018-09-09 11:38
    关注

    C语言要求先定义后使用,所以要把函数的定义和结构体的定义放在前面。

    不考虑程序本身的对错,可以编译运行的修改如下:

    #include <stdio.h>
    #include <malloc.h>
    #define maxsize 20
    
    typedef struct BTNode{
    int data;
    struct BTNode *lchild,*rchild;
    }BTNode;
    
    typedef struct
    {
      BTNode *q;
      int level;
    }St;
    
    St A[maxsize];
    
    int L=1;//记录层数
    int i=0;
    void width(BTNode *p);
    BTNode *CreateBiTree();
    
    
    //先序建立二叉树
    BTNode *CreateBiTree(){
    char ch;
    BTNode *T;
    scanf("%c",&ch);
    if(ch=='#')T=NULL;
    else{
    T = (BTNode *)malloc(sizeof(BTNode));
    T->data = ch;
    T->lchild = CreateBiTree();
    T->rchild = CreateBiTree();
    }
    return T;//返回根节点
    }
    
    void width(BTNode *p)
    {
    
      A[i].q=p;
      A[i].level=L;
      ++i;
      ++L;
      width(p->lchild);
      width(p->rchild);
      --L;
    
    }
    
    int main()
    {
       int j,k=0;
            int max=0;
      BTNode * T;
      T = CreateBiTree();//建立
      width(T);
    
    
            for(j=0;j<=i;j++)
            {
              if(A[j].level==A[j+1].level)
              {
                ++k;
                    max=(k>max?k:max);
              }
              else
                      k=1;
            }
            printf("%d\n",max);
    
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退