本题要求实现一个函数,按照后序遍历的顺序输出给定二叉树中度为2的结点。
函数接口定义:
void PostorderPrintNodes( BiTree T);
T是二叉树树根指针,PostorderPrintNodes按照后序遍历的顺序输出给定二叉树T中度为2的结点,格式为一个空格跟着一个字符。
其中BiTree结构定义如下:
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode
{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
BiTree Create();/* 细节在此不表 */
void PostorderPrintNodes( BiTree T);
int main()
{
BiTree T = Create();
printf(“Nodes are:”);
PostorderPrintNodes(T);
return 0;
}
/* 你的代码将被嵌在这里 */
输出样例(对于图中给出的树):
Nodes are: D A
void PostorderPrintNodes( BiTree T)
{
if(T==NULL){
return 0;
}
else {
PostorderPrintNodes( T->lchild );//一直往左
PostorderPrintNodes( T->rchild );//一直往右
if(T->lchild!=NULL&&T->rchild!=NULL ){//中间
printf(" %c",T->data );
}
}
}