非遞迴演算法實現二叉樹高度

來源:互聯網
上載者:User

標籤:演算法實現   出隊   中序遍曆   開始   先序遍曆   中序   語言   演算法   後序   

思路:

嘻嘻,請讀者自己手動類比。博主這裡不知道怎麼用語言說。

拓展:

演算法思路適用於

(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

非遞迴演算法實現二叉樹高度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.