二叉树的遍历:先序、中序、后序
先序遍历的算法
- 先访问根结点
- 先序遍历左子树
- 先序遍历右子树
void PreOrder(BiTree root)
{
if(root!=NULL)
{
printf("%c",root->data);
PreOrder(root->lchild);
PreOrder(root->rchild);
}
}
中序遍历的算法
- 中序遍历左子树
- 访问根结点
- 中序遍历右子树
void InOrder(BiTree root)
{
if(root!=NULL)
{
InOrder(root->lchild); //访问左子树
printf("%c",root->data); //访问根结点
InOrder(root->rchild); //访问右子树
}
}
后序遍历的算法
- 后序遍历左子树
- 后序遍历右子树
- 访问根结点
void PostOrder(BiTree root)
{
if(root!=NULL)
{
PostOrder(root->lchild);
PostOrder(root->rchild);
printf