#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