- /** 二叉树深度的递归算法 */ 
- int depth(BTree root)
- {
- int ldepth,rdepth;
- if(!root)     return 0;
- else{
- ldepth = depth(root->lchild);
- rdepth = depth(root->rchild);
- return ldepth>rdepth?ldepth+1;rdepth+1;
- }
- } 
- /** 二叉树深度的非递归算法(在中根遍历算法的基础上修改的来)
- http://blog.csdn.net/panqiaomu/archive/2007/05/17/1612556.aspx */
- int depth2(BTree root){
- int top = 0;
- int depth = 0,temp = 0;//temp 保存当前单分支的最大值
- BTree p = root;
- BTree nodePointer[MAX_TREE_DEGREE];
- while(p || top > 0){
- if(p){
- nodePointer[top] = p;
- ++ top;
- ++ depth; //统计单分支的深度 
- p = p->lchild;
- }else{
- -- top;
- p = nodePointer[top];
- p = p->rchild;
- if(p == NULL){ //单分支结束
- if(depth > temp )
- temp = depth;
- -- depth;
- }
- }//else
- }//while
- return temp;
- }
 
		 
		posted @ 
2011-06-01 09:16 
itbird 
Views(
13623) 
Comments() 
 
收藏 
举报