#include"stdio.h"
#include"stdlib.h"
struct tree
{
char data
;
struct tree
*lchild
;
struct tree
*rchild
;
};
void createtree(struct tree
**T
)
{
char c
;
scanf("%c", &c
);
if(' ' == c
)
{
*T
= NULL;
}
else
{
*T
= (struct tree
*)malloc(sizeof(struct tree
));
(*T
)->data
= c
;
createtree(&(*T
)->lchild
);
createtree(&(*T
)->rchild
);
}
}
void visit(char c
, int level
)
{
printf("%c在第%d层\n", c
, level
);
}
void traversebtree(struct tree
*T
, int level
)
{
if( T
)
{
visit(T
->data
, level
);
traversebtree(T
->lchild
, level
+1);
traversebtree(T
->rchild
, level
+1);
}
}
int main()
{
struct tree
*T
;
createtree(&T
);
traversebtree(T
, 1);
return 0;
}
转载请注明原文地址:https://tech.qufami.com/read-757.html