《二叉树深度的递归算法.docx》由会员分享,可在线阅读,更多相关《二叉树深度的递归算法.docx(1页珍藏版)》请在三一办公上搜索。
二叉树深度的递归算法/二叉树深度的递归算法 int depth(BTree root) int ldepth,rdepth; if(!root) return 0; else ldepth = depth(root-lchild); rdepth = depth(root-rchild); return ldepthrdepth?ldepth+1;rdepth+1; / 二叉树深度的非递归算法 int depth2(BTree root) int top = 0; int depth = 0,temp = 0; /temp 保存当前单分支的最大值 BTree p = root; BTree nodePointerMAX_TREE_DEGREE; while(p | top 0) if(p) nodePointertop = p; + top; + depth; /统计单分支的深度 p = p-lchild; else - top; p = nodePointertop; p = p-rchild; if(p = NULL) /单分支结束 if(depth temp ) temp = depth; - depth; /else /while return temp;