數據結構實驗—求二叉樹高度(二叉鏈表存儲)


#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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM