
该方法,将其转成二叉树并用前序输出即为后序转前序的方法
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct tree
{
char no;
struct tree* left;
struct tree* right;
}TREE;
typedef struct tree* Tree;
typedef struct queue
{
struct tree* Array[100];
int front;
int rear;
}QUEUE;
//typedef struct queue* Queue;
QUEUE Q;
void Push(Tree T)
{
Q.Array[Q.rear++]=T;
}
Tree Pop()
{
return Q.Array[--Q.rear];
}
void Preorder(Tree root)
{
if(root!=NULL)
{
printf("%c ",root->no);
Preorder(root->left);
Preorder(root->right);
}
}
int main()
{
Tree T=NULL;
Tree root=NULL;
char initial[100];
int length=0;
Tree tempr, templ;
fgets(initial,99,stdin);
length=strlen(initial);
for(int i=0;i<length;i++)
{
if(initial[i]>='0'&&initial[i]<='9')
{
if(root==NULL)
{
T=(Tree)malloc(sizeof(struct tree));
T->no=initial[i];
T->left=NULL;
T->right=NULL;
root=T;
}
else
{
T=(Tree)malloc(sizeof(struct tree));
T->no=initial[i];
T->left=NULL;
T->right=NULL;
}
Push(T);
}
if(initial[i]=='+'||initial[i]=='-'||initial[i]=='*'||initial[i]=='/')
{
tempr=Pop();//出栈后放入右节点//
templ=Pop();//出栈后放入左节点//
T=(Tree)malloc(sizeof(struct tree));
T->no=initial[i];
T->left=templ;
T->right=tempr;
root=T;
Push(T);
}
}
Preorder(root);
return 0;
}
1553

被折叠的 条评论
为什么被折叠?



