数据结构实验—求二叉树高度(二叉链表存储)

#include<stdio.h>
#include<stdlib.h>
typedef struct Bitnode
{
    int data;
    struct Bitnode *left,*right;
}Bitnode;
Bitnode *CreatBitree_level()
{
    int x,front=0,tail=0;
    Bitnode *root=NULL,*p,*qu[1001];
    while(scanf("%d",&x),x!=-1)
    {
        if(x==0)
        p=NULL;
        else 
        {
            p=(Bitnode*)malloc(sizeof(Bitnode));
            p->data=x;
            p->left=p->right=NULL;
        }
        qu[++tail]=p;
        if(tail==1)
        root=p;
        else
        {
            if(qu[front]!=NULL&&p)
            {
                if(tail%2==0)
                qu[front]->left=p;
                else
                qu[front]->right=p;
            }
        }
        if(tail%2==1)
        front++;
    }return root;
}
int depth(Bitnode *t)
{
    int l=0,r=0;
    if(t==NULL)
    return 0;
        l=depth(t->left)+1;
        r=depth(t->right)+1;
if(l>r)
return l;
else
return r;
}

这题就用递归来做,高度为左右子数中高度最高的+1

猜你喜欢

转载自www.cnblogs.com/zzjam--1/p/11332302.html