首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

小弟我的程序中序遍历哪错了,如何结果不对啊

2012-02-08 
我的程序中序遍历哪错了,怎么结果不对啊?#includestdio.h#includestdlib.hstruct TreeNode{int iDate

我的程序中序遍历哪错了,怎么结果不对啊?
#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);
}

热点排行