小弟我的程序中序遍历哪错了,如何结果不对啊
我的程序中序遍历哪错了,怎么结果不对啊?
#include<stdio.h>
#include<stdlib.h>
struct TreeNode
{
int iDate;
TreeNode* pLeft;
TreeNode* pRight;
};
/*递归*/
TreeNode* CreateRest_BTree()
{
TreeNode* pBtree;
int iNum;
scanf("%d",&iNum);
if(iNum == -1)
{
pBtree = NULL;
}
else
{
pBtree = (TreeNode*)malloc(sizeof(TreeNode));
pBtree->iDate = iNum;
pBtree->pLeft = CreateRest_BTree();
pBtree->pRight = CreateRest_BTree();
}
return pBtree;
}
/*建立二叉树*/
TreeNode* CreateBTree()
{
TreeNode* pBtree;
pBtree = (TreeNode*)malloc(sizeof(TreeNode));
pBtree = CreateRest_BTree();
return pBtree;
}
void Vist(TreeNode* p)
{
printf("%d ",p->iDate);
}
/*先序遍历*/
void PreOrder(TreeNode* p)
{
if(p == NULL)
{
return;
}
Vist(p);
PreOrder(p->pLeft);
PreOrder(p->pRight);
}
/*中序遍历*/
void inOrder(TreeNode* p)
{
if(p == NULL)
{
return;
}
PreOrder(p->pLeft);
Vist(p);
PreOrder(p->pRight);
}
int main()
{
TreeNode* pBtree;
pBtree = (TreeNode*)malloc(sizeof(TreeNode));
pBtree = CreateBTree();
//PreOrder(pBtree);
printf("\n");
inOrder(pBtree);
getchar();
getchar();
}
------解决方案--------------------
void inOrder(TreeNode* p)
{
if(p == NULL)
{
return;
}
inOrder(p->pLeft);
Vist(p);
inOrder(p->pRight);
}
#include<stdio.h>
#include<stdlib.h>
struct TreeNode
{
int iDate;
TreeNode* pLeft;
TreeNode* pRight;
};
/*递归*/
TreeNode* CreateRest_BTree()
{
TreeNode* pBtree;
int iNum;
scanf("%d",&iNum);
if(iNum == -1)
{
pBtree = NULL;
}
else
{
pBtree = (TreeNode*)malloc(sizeof(TreeNode));
pBtree->iDate = iNum;
pBtree->pLeft = CreateRest_BTree();
pBtree->pRight = CreateRest_BTree();
}
return pBtree;
}
/*建立二叉树*/
TreeNode* CreateBTree()
{
TreeNode* pBtree;
pBtree = (TreeNode*)malloc(sizeof(TreeNode));
pBtree = CreateRest_BTree();
return pBtree;
}
void Vist(TreeNode* p)
{
printf("%d ",p->iDate);
}
/*先序遍历*/
void PreOrder(TreeNode* p)
{
if(p == NULL)
{
return;
}
Vist(p);
PreOrder(p->pLeft);
PreOrder(p->pRight);
}
/*中序遍历*/
void inOrder(TreeNode* p)
{
if(p == NULL)
{
return;
}
PreOrder(p->pLeft);
Vist(p);
PreOrder(p->pRight);
}
int main()
{
TreeNode* pBtree;
pBtree = (TreeNode*)malloc(sizeof(TreeNode));
pBtree = CreateBTree();
//PreOrder(pBtree);
printf("\n");
inOrder(pBtree);
getchar();
getchar();
}
------解决方案--------------------
void inOrder(TreeNode* p)
{
if(p == NULL)
{
return;
}
inOrder(p->pLeft);
Vist(p);
inOrder(p->pRight);
}