思路:
嘻嘻,請讀者自己手動模擬。博主這里不知道怎么說。
拓展:
該算法思路也適用於
(1)每層的結點個數
(2)樹的最大寬度
(3)節點位於某一層
int height(BiTree T){
if(T==null)
return 0;
int front=-1, rear=-1;//front 出隊指針 rear 入隊指針
int last = 0, level=0;//last 每一層的最右指針(front==last時候一層遍歷結束 level++)
BiTree Q[Maxsize];//模擬隊列
Q[++rear] = T;
BiTree p;
while(front<rear){
p = Q[++front];//開始出隊 因為front要趕到lash 實現level++
if(p->lchild)
Q[++rear] = p->lchild;
if(p->rchild)
Q[++rear] = p->rchild;
if(front==last){
level++;
last=rear;//last指向下層節點
}
}
}
您可能感興趣的- 非遞歸先序遍歷二叉樹https://www.cnblogs.com/Coeus-P/p/9353186.html
- 非遞歸后序遍歷二叉樹版本二https://www.cnblogs.com/Coeus-P/p/9354754.html
- 遞歸算法--二叉樹寬度https://www.cnblogs.com/Coeus-P/p/9354671.html
- 遞歸算法--交換二叉樹左右子樹https://www.cnblogs.com/Coeus-P/p/9353568.html
- 遞歸算法--二叉樹高度https://www.cnblogs.com/Coeus-P/p/9353528.html
- 遞歸算法--二叉樹中葉子結點https://www.cnblogs.com/Coeus-P/p/9353505.html
- 遞歸算法--二叉樹中度為2的結點https://www.cnblogs.com/Coeus-P/p/9353495.html
- 遞歸算法--二叉樹中度為1的結點https://www.cnblogs.com/Coeus-P/p/9353484.html
- 非遞歸實現斐波那契數列https://www.cnblogs.com/Coeus-P/p/9353452.html
- 非遞歸后序遍歷二叉樹版本一https://www.cnblogs.com/Coeus-P/p/9353360.html
- 層次遍歷二叉樹https://www.cnblogs.com/Coeus-P/p/9353257.html
- 非遞歸中序遍歷二叉樹https://www.cnblogs.com/Coeus-P/p/9353227.html
- 非遞歸先序遍歷二叉樹https://www.cnblogs.com/Coeus-P/p/9353186.html