根据二叉树先序遍历和中序遍历的特点,采用递归的方法求解。
程序1(通过根节点的位置判断树的左右子树是否为空):
//已知二叉树的中序和先序遍历,求二叉树的后续遍历
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct BiTree
{
char data;
struct BiTree *left,*right;
}BiNode; //定义结点结构
//通过前序遍历和中序遍历创建二叉树
void InitTree(BiNode *&root,char *front,char *middle,int num)
{
int i=0;
root->data=front[0];
if (num==0)
return ;
//找到根结点位置
for (i=0;i<num;i++)
{
if (middle[i]==root->data)
break;
}
//如果有左子树
if(i!=0)
{
root->left=new BiNode(); //BiNode之后的()可省略
InitTree(root->left,front+1,middle,i);
}
else
root->left=NULL;
//如果有右子树
if(i!=num-1)
{
root->right=new BiNode();
InitTree(root->right,front+i+1,middle+i+1,num-i-1);
}
else
root->right=NULL;
}
//后序遍历
void PostOrder(BiNode *root)
{
if (root==NULL)
return;
PostOrder(root->left);
PostOrder(root->right);
printf("%c ",root->data);
}
//main函数,测试
int main()
{
BiNode *root=new BiNode;
char *front="ABDECF",*mid="DBEAFC";
int num=strlen(front);
InitTree(root,front,mid,num);
PostOrder(root);
system("pause");
return 0;
}
程序2(与程序1类似,需要手动输入二叉树的前序和中序,不判断左右子树是否为空,通过len的值是否小于等于0,将叶子结点的左右子树置为NULL)
转载出处http://blog.csdn.net/Java2King/article/details/5564418:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char ch;
struct node *left,*right;
}node; // 定义节点的结构
node * creat(char *pre,char *in,int len);
void print(node *head);
int main()
{
int len;
char pre[30],in[30]; // 存储先序和中序遍历的序列
node *head;
head=(node*)malloc(sizeof(node));
while(scanf("%s%s",pre,in)!=EOF)
{
len=strlen(pre);
head=creat(pre,in,len);
print(head);
printf("/n");
}
system("pause");
return 0;
}
// 通过前序遍历和中序遍历创建二叉树
node * creat(char *pre,char *in,int len)
{
int k;
if(len<=0) return NULL;
node *head=(node*)malloc(sizeof(node));
head->ch=*pre;
char *p;
for(p=in;p!=NULL;p++) // 在中序遍历的序列中得到与先序相同的节点
if(*p==*pre) break; //即找到根结点在中序遍历的位置
k=p-in;
head->left=creat(pre+1,in,k); //递归调用得到左子树
head->right=creat(pre+k+1,p+1,len-k-1);//得到右子树
return head;
}
// 打印后序遍历序列
void print(node *head)
{
if(head==NULL) return ;
print(head->left);
print(head->right);
printf("%c",head->ch);
}
程序3. 不建立树,仅输出后序遍历
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct TreeNode
{
struct TreeNode* left;
struct TreeNode* right;
char elem;
};
void BinaryTreeFromOrderings(char* inorder, char* preorder, int length)
{
if(length == 0)
{
return ;
}
// TreeNode* node = new TreeNode;//Noice that [new] should be written out.
char node_value;
node_value = *preorder;
int rootIndex = 0;
for(;rootIndex < length; rootIndex++)//a variation of the loop
{
if(inorder[rootIndex] == *preorder)
break;
}
BinaryTreeFromOrderings(inorder, preorder +1, rootIndex);
BinaryTreeFromOrderings(inorder + rootIndex + 1, preorder + rootIndex + 1, length - (rootIndex + 1));
std::cout<<node_value<<std::endl;
return ;
}
int main(int argc, char** argv)
{
char* pr="GDAFEMHZ";
char* in="ADEFGHMZ";
BinaryTreeFromOrderings(in, pr, 8);
printf("\n");
system("pause");
return 0;
}
参考原网页:http://blog.csdn.net/feliciafay/article/details/6816871