關於二叉樹的深度演算法題目及解答

來源:互聯網
上載者:User

求二叉樹的深度演算法

演算法的思想:

  採用二叉樹的後序遍曆非遞迴演算法。由於後序遍曆非遞迴演算法使用一個棧實現,每次都會在一條路徑上走到最底層才向上訪問,再向右訪問。因此,記錄下棧在遍曆中的最大值,即為二叉樹的最大深度。

#include <iostream>#include <stack>using namespace std;struct BinTree{    int data;    BinTree *lc;    BinTree *rc;}BTNode,*BinTree;int max(int a,int b){    return (a>b)?a:b;}int BinTreeDepth(BinTree T){    stack<BinTree> s;    BinTree p = T,r = NULL;    int depth=0;    while(p||!s.empty())    {        if(p)        {    //從根節點向左邊走            s.push(p);            int size = s.size();//擷取棧的大小            depth = max(depth,size);//替換最大值            p = p->lc;        }        else        {    //左邊走不通向右走            p = s.top();            if(p->rc&&p->rc!=r)//如果右子樹存在,且未被訪問過            {                p = p->rc;                s.push(p);                int size = s.size();//擷取棧的大小                depth = max(depth,size);//替換最大值                p = p->lc;            }else            {                p=s.top();                s.pop();                cout<<p->data<<endl;                r=p;            //記錄最近訪問的節點                p=NULL;            //節點訪問完之後,重設p指標,目的是為了防止再次將左孩子壓棧            }        }    }    return depth;}


求二叉樹深度的演算法

題目:二叉樹用二叉鏈表表示,編寫求二叉樹深度的演算法。


答案是:

int height(Bitree T){  if (T==NULL) return 0;  u=height(T->lchild);  v=height(T->rchild);   if (u>n) return (u+1)  return (v+1)}


關於遞迴,你可以看成是一句一句往下運行嘛。需要儲存狀態的時候,系統就會自動用棧幫你儲存。就依你說得那個為例:
n為全域變數,初值為0;

第一次調用height(T),假設T!=NULL
由於T!=NULL:跳過if (T==NULL) return 0;

關鍵到了u=height(T->lchild); 調用本身的函數:此時的T->lchild儲存在棧中,既然是調用就得從函數開頭執行:
看下這時候T2(其實就是T->lchild),if (T==NULL) return 0;
這裡假設T不是NULL,就繼續運行在遇到u=height(T->lchild); 在調這個函數本身——
這裡就假設它為T->lchild==NULL吧。這下可好,這個函數執行return 0;

慢著:第二次函數調用u=height(T->lchild)中的函數值已經計算出來啦。這時u=0;

你還記得第二次調用運行到了v=height(T->rchild); 這句話吧?好,這個過程就和u=height(T->lchild)完全一樣。
這裡也假設得到的v=0

則第二次調用到了if (u>n) return (u+1)
return (v+1)
得到一個傳回值,不如就假設u〉n,於是傳回值1;
好,這一波完畢;

你還記得第一次調用的height吧,這時把傳回值給u,u=1;
然後執行到第一次調用中的v=height(T->rchild); 了。分析同上。

這個過程的確比較複雜。慢慢琢磨吧。呵呵。

 基本思路就是如果當前節點還有子節點,則繼續訪問,遞迴的找尋子節點直到葉子節點為止。

procedure tree(a:node,depth:integer); begin if result<depth then result:=depth; if a.leftchild<>nil then tree(a.leftchild,depth+1); if a.rightchild<>nil then tree(a.rightchild,depth+1); end;

注:result是全域變數,是結果

實際上最好不用什麼全域變數 

int depth(node *bt) { if (bt==NULL) return 0; ldepth=depth(bt->left)+1; rdepth=depth(bt->right)+1; return max(ldepth,rdepth); }


全域變數是bug 

int Height(BiTree T){ int m,n; if(!T) return(0); else m=Height(T->lchild); n=Height(T->rchild); return((m>n?m:n)+1); }


求樹深度的遞迴演算法 

// struct bnode{struct bnode *lc,*rc); int depth(struct bnode *r) { return r==NULL?0:1+max(depth(r->lc),depth(r->rc)); }


問:設計演算法求二叉樹的深度

設一棵二叉樹以二叉鏈表格儲存體,試設計演算法求此二叉樹的深度

回答:

#include <stdio.h>#include <stdlib.h>typedef struct node{ char data; struct node *left,*right;}Node,*PNode;PNode createBtree(PNode root)//建立二叉樹,控制台下輸入,基於先序遍曆輸入{   char data;   scanf("%c",&data);   if (data==' ')   {    root=NULL;    return root;   }   root = (PNode)malloc(sizeof(Node));   root->data = data;   root->left = createBtree(root->left);   root->right = createBtree(root->right);   return root;}int depth(PNode root)//這就是你要的函數。{ int ld,rd; if (root==NULL) {  return 0; } ld = 1+depth(root->left); rd = 1+depth(root->right); return ld>rd?ld:rd;}int main(){    PNode root=NULL; root = createBtree(root); printf("%d",depth(root)); return 0;}


為了測試,寫了二叉樹的建立程式;
如下輸入可以看到結果
虛節點用空格輸入的。例如你輸入
先序遍曆
234空格空格5空格6空格空格7空格空格斷行符號就可以看到結果。
另外,本演算法是從1開始算深度的,就是根節點是深度下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.