(1)判斷是否為完全二叉樹 (2)求二叉樹的高度

2021-08-09 07:45:43 字數 1418 閱讀 9467

歡迎加qq群:453398542 學習討論,會定期分享資料課程,解答問題。

(1)判斷是否為完全二叉樹

(2)求二叉樹的高度

#include

#include

#define size 100

typedef struct bitnodebitnode, *bitree;

//佇列 

typedef struct sqqueue;

void initqueue(sqqueue &q) //佇列

void creatbitree(bitree &bt){

//構造二叉樹

char ch;

ch=getchar();

if(ch=='#')

bt=null;

else{

bt=(bitree)malloc(sizeof(bitnode));

bt->data=ch;

creatbitree(bt->lchild);

creatbitree(bt->rchild);

int depth(bitree bt){

//求二叉樹的高度

int hl,hr;

if(!bt){

return 0;

else{

hl=depth(bt->lchild)+1;

hr=depth(bt->rchild)+1;

return hl>hr?hl:hr;

int judge(bitree bt){

//判斷是否為完全二叉樹

sqqueue q;

bitree p;

int flag=1;

if(bt){

initqueue(q);

enqueue(q,bt);

while(!queueempty(q)&&flag){

dequeue(q,p);

if(p){

enqueue(q,p->lchild);

enqueue(q,p->rchild);

else{

while(!queueempty(q)&&flag){

dequeue(q,p);

if(p){

flag=0;

return flag;

int main(){

int h;

bitree bt;

printf("輸入資料:\n");

creatbitree(bt);

h=depth(bt);

printf("樹的高度為:%d\n",h);

if(judge(bt)){

printf("是完全二叉樹\n");

else{

printf("不是完全二叉樹\n");

return 0;

判斷二叉樹是否為完全二叉樹

include include 節點 struct node 銷毀二叉樹 void destroy tree node root destroy tree root left destroy tree root right delete root 是否為完全二叉樹 bool is cbtree no...

判斷二叉樹是否為完全二叉樹

判斷二叉樹是否為完全二叉樹 完全二叉樹看起來就像是滿二叉樹右下角缺了一口。思路 需要引入乙個標誌位來區分兩個階段 1.先對該樹進行層序遍歷,會出現兩種階段 a 任何乙個節點都有兩顆子樹 當遇到乙個結點沒有子樹或者只有左子樹時,那麼就進入第二階段。當遇到乙個節點只有右子樹時,那麼這棵樹一定不是完全二叉...

二叉樹 判斷二叉樹是否為完全二叉樹

問題描述 判斷一棵二叉樹是否為完全二叉樹。知識點 完全二叉樹是指除二叉樹的最後一層外,其他各層的節點數達到最大個數,且最後一層的葉節點從左到右連續存在,只缺右側若干節點。演算法實現 class node is complete binary tree public static boolean is...