【发布时间】:2015-02-03 01:07:05
【问题描述】:
对不起,我的英语不好。
我需要打印一般树或 n 叉树的层级。
树的结构是:
struct GTnode{
int data;
nodeGT *fc; //first child
nodeGT *nb; //next brother
}
算法的难点在于当你在同一关卡中有 2 个不同的节点并且每个节点都有一个子节点。
编辑 例如,如果我有这棵树:
1
2 7 8
3 6 9 12
4 5 10 11
我必须打印:
1
2 7 8
3 6 9 12
4 5 10 11
每个endl代表树中的不同层
编辑 我的代码的一个想法是下一个:
void printLevel(GTnode *root){
GTnode *aux = root;
if(root != NULL){
cout<<aux->data;
printLevel(aux->nb);
cout<<end; //Print the space between levels
printLevel(aux->fc);
}
}
我知道这是错误的,但这只是我所拥有的想法。
【问题讨论】: