当前位置:文档之家› 第四组 统计二叉树的叶子结点的个数

第四组 统计二叉树的叶子结点的个数


第4页
非递归算法
void printTree(BitTree T) { \\初始化栈 Stack s; s=(Stack*)malloc(sizeof(Stack *)); s->top=0; while(T!=null && s->top!=0){ if(T!=null){ printf(T->data); s->Maxsize[s->top]=T->data; s->top++; T=
主讲内容
• 统计二叉树的叶子结点个数算法的基 本思想 • 算法解读
第2页
统计二叉树中叶子结点的个数算法基本思想
• 先序(或中序或后序)遍 历二叉树,在遍历过 程中查找叶子结点, 并计数。由此,需在 遍历算法中增添一个 “计数”的参数,并 将算法中“访问结点” 的操作改为:若是叶 子,则计数器增1。
第5页
else { T=s->Maxsize[s->top]; s->top--; if(T->lchild==null && T->rchild==null) { w++;} T=T->rchild; } } }
第6页
谢谢!
第7页
第3页
递归算法
void CountLeaf (BiTree T, int& count){ if ( T ) { if ((!T->lchild)&& (!T->rchild)) count++; // 对叶子结点计数 CountLeaf( T->lchild, count); CountLeaf( T->rchild, count); } // if } // CountLeaf
相关主题