二叉樹深度求解(遞迴,非遞迴)
阿新 • • 發佈:2019-02-14
1 int TreeDeep(BinTree BT ){
2 int treedeep=0;
3 stack S;
4 stack tag;
5 BinTree p=BT;
6 while(p!=NULL||!isEmpty(S)){
7 while(p!=NULL){
8 push(S,p);
9 push(tag,0);
10 p=p->lchild;
11 }
12 if(Top(tag)==1){
13 deeptree=deeptree>S.length?deeptree:S.length;
14 pop(S);
15 pop(tag);
16 p=NULL;
17 }else{
18 p=Top(S);
19 p=p->rchild;
20 pop(tag);
21 push(tag,1);
22 }
23 }
24 return deeptree;
25 }