C语言实现后序遍历(递归)

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

typedef struct ThrNode
{
   int data;
   struct ThrNode *lchild;
   struct ThrNode *rchild;
   struct ThrNode *parent;
}thrnode;

bool CreateThrTree(thrnode* &T,thrnode* p)
{   
	
	char ch;
	scanf("%c",&ch);
	if(ch=='m') 
	{
		T=NULL;
		
	}
	else
	{
		T=(thrnode*)malloc(sizeof(thrnode));
		T->data=ch;
		T->parent=p;
		CreateThrTree(T->lchild,T);
		CreateThrTree(T->rchild,T);
	}

  return true;
}
void PrintTree(thrnode* T)
{
  printf("%c",T->data);

}

void PostOrderTraverse(thrnode* T)
{
  if(T)
  {
     PostOrderTraverse(T->lchild);
     PostOrderTraverse(T->rchild);
	 PrintTree(T);
  }
  
}
void main()
{
  thrnode *head=(thrnode*)malloc(sizeof(thrnode));
  head->data=0;
  head->parent=NULL;
  head->lchild=NULL;
  if(CreateThrTree(head->lchild,head)) PostOrderTraverse(head->lchild);
  

}

 

你可能感兴趣的:(C++,c,C#)