二叉樹的三大遍歷與求高度和結點

2021-08-11 08:08:30 字數 964 閱讀 8934

#includetypedef struct nodebitnode,*bitree;

void createbitree(bitree *bitree)

}void preorder(bitree root)

}inorder(bitree root)

/*中序遍歷二叉樹, root為指向二叉樹(或某一子樹)根結點的指標*/

}void postorder(bitree root)

/* 後序遍歷二叉樹,root為指向二叉樹(或某一子樹)根結點的指標*/

} void preorder1(bitree root) /* 先序遍歷輸出二叉樹結點, root為指向二叉樹根結點的指標 */

}void preorder2(bitree root) /* 先序遍歷輸出二叉樹中的葉子結點 , root為指向二叉樹根結點的指標 */

}/*採用遞迴演算法,如果是空樹,返回0;如果只有乙個結點,返回1;否則為左右子樹的葉子結點數之和。*/

int leaf(bitree root)

int posttreedepth(bitree bt) /* 後序遍歷求二叉樹bt高度的遞迴演算法 */

else return(0); /* 如果是空樹,則返回0 */

}int hmax=0;

// 先序遍歷求二叉樹bt高度的遞迴演算法,h為bt指向結點所在層次,初值為1

//depth為當前求得的最大層次,為全域性變數,呼叫前初值為0

void pretreedepth(bitree bt, int h)

pretreedepth(bt->lchild, h+1); /* 遍歷左子樹 */

pretreedepth(bt->rchild, h+1); /* 遍歷右子樹 */

}int main()

/*abde..f...cgi...hj..k.l..*/

求二叉樹高度

函式介面定義 int getheight bintree bt 其中bintree結構定義如下 typedef struct tnode position typedef position bintree struct tnode 要求函式返回給定二叉樹bt的高度值。裁判測試程式樣例 include...

求二叉樹高度

本題要求給定二叉樹的高度。函式介面定義 int getheight bintree bt 其中bintree結構定義如下 typedef struct tnode position typedef position bintree struct tnode 要求函式返回給定二叉樹bt的高度值。裁判測...

求二叉樹高度

題目來自於pta 函式介面定義 int getheight bintree bt 其中bintree結構定義如下 typedef struct tnode position typedef position bintree struct tnode 要求函式返回給定二叉樹bt的高度值。裁判測試程式樣...