20分求解!!!关于二叉树的先,中,后序遍历结果出错(c语言实现)

来源:百度知道 编辑:UC知道 时间:2024/05/27 11:17:41
程序能运行,但是不能得到真确的(中后序)结果.请各位高手在1点前帮我解决,谢谢了.
请附上你输入的二叉树和运行结果,最好能给个二叉树的图形.
........
typedef struct node{
char data;
struct node *lchild,*rchild;
}BinTNode;
typedef BinTNode *BinTree;

BinTree CreatBinTree(void)
{
BinTree T;
char ch;
if((ch=getchar())=='#')
return(NULL);
else{
T=(BinTNode *)malloc(sizeof(BinTNode));
T->data=ch;
T->lchild=CreatBinTree();
T->rchild=CreatBinTree();
return(T);
}
}
void Preorder(BinTree T)
{
if(T!=NULL) {
printf("%c",T->data);
Preorder(T->lchild);
Preorder(T->rchild);
}
}

void Inorder(BinTree T)
{
if(T!=NULL) {
Preorder(T->lchild);
printf("%c",T->data);
Preorder(T->rchild);
}<

你的代码中
void Inorder(BinTree T)
{
if(T!=NULL) {
Preorder(T->lchild);
printf("%c",T->data);
Preorder(T->rchild);
}
}
改为
void Inorder(BinTree T)
{
if(T!=NULL) {
Inorder(T->lchild);
printf("%c",T->data);
Inorder(T->rchild);
}
}
就可以了。

×××××××××××××××××××××××××××××××××××××××××

#include <stdio.h>
#include <malloc.h>

#define null 0
struct CTreeNode
{
/*数据*/
char data;
/*左孩子*/
struct CTreeNode* LeftChild;
/*右孩子*/
struct CTreeNode* RightChild;
};

/*访问结点*/
void VisitNode(struct CTreeNode *pNode)
{
printf("%2c ", pNode->data);
}

/*先序遍历*/
void PreOrderTraversal(struct CTreeNode *pNode)
{
VisitNode(pNode);

struct CTreeNode *pLChild = pNode->LeftChild;
if (pLChild !