二叉树的后序遍历
二叉树的遍历一般都是已知后序或前序的一个,再加上中序,至于为什么在这就不赘述了。
如题:
Problem Description
输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。
Input
第一行输入二叉树的先序遍历序列;
第二行输入二叉树的中序遍历序列。
Output
输出该二叉树的后序遍历序列。
Example Input
ABDCEF
BDAECF
Example Output
DBEFCA
这个题就是简单的知道前序、中序,求后序。你要对前序遍历和中序遍历有着熟悉的了解,看看代码。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
char data;
struct node *left,*right;
};
struct node *creat(int n,char *str1,char *str2)
{
struct node *root;
int i;
if(n==0)
return NULL;
root=(struct node *)malloc(sizeof(struct node));
root->data=str1[0];
for(i=0;i<n;i++)
{
if(str2[i]==str1[0])
break;
}
root->left=creat(i,str1+1,str2);
root->right=creat(n-i-1,str1+i+1,str2+i+1);
printf("%c",root->data);
return root;
};
int main()
{
int n;
char str1[1100],str2[1100];
scanf("%s",str1);
scanf("%s",str2);
n=strlen(str1);
creat(n,str1,str2);
printf("\n");
return 0;
}
/***************************************************
User name: LAN7
Result: Accepted
Take time: 0ms
Take Memory: 112KB
Submit time: 2017-02-07 14:36:35
****************************************************/
好啦,creat函数就是用来构建这棵二叉树,然后再通过递归调用该函数,不断的找出根节点的左右孩子,找到根节点后,再找到它的左右孩子,然后让孩子作为根,再找它们的孩子。这让我想起来快排,找到标准后,分为左右两半,左右再重复这个过程,用的就是分治递归思想,不断拆分数据,使它们有序。
root->data=str1[0];
for(i=0;i<n;i++)
{
if(str2[i]==str1[0])
break;
}
来看看这一段代码,str1[0]是它的根节点,因为熟悉前序遍历的话你就知道,在对该树的前序遍历中,第一个永远都是这棵树的根节点,然后,通过一个for循环,我们在中序遍历中可以找到这个根节点的位置,然后我们就可以通过中序遍历的性质知道在该树的中序遍历中,根节点的左部分为其左子树部分,而右部分为右子树部分。
root->left=creat(i,str1+1,str2);
root->right=creat(n-i-1,str1+i+1,str2+i+1);
所以呢,第一层,我们区分了最上层的那个根,以及它的左子树,右子树,然后这两句开始递归。中序遍历中根节点左部分为它左子树,长度即为i,总长度为n,所有右部分 = 总 - 左 - 根 即为:n - i -1。因为前序遍历中第一个必为根节点,那么我们递归的时候再分别把左孩子,右孩子左为根节点,再求它们的子树。 str+1,即为左子树在前序遍历中的起始位置,str2必为其最左边的起始位置,同理可以得到右子树在str1,str2中的位置。这样,通过递归就可以得到整棵树了。
二叉树的前序遍历
这个只不过已知把前序改为后序
如题:
Problem Description
已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
Input
输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的中序遍历序列,第二个字符串表示二叉树的后序遍历序列。
Output
输出二叉树的先序遍历序列
Example Input
2
dbgeafc
dgebfca
lnixu
linux
Example Output
abdegcf
xnliu
然后呢,只要你把前面那个弄明白之后就可以啦,这个就十分简单了。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
char data;
struct node *left,*right;
};
struct node *creat(int n,char *str1,char *str2)
{
struct node *root;
int i;
if(n==0)
return NULL;
root=(struct node *)malloc(sizeof(struct node));
root->data=str2[n-1];
printf("%c",root->data);
for(i=0;i<n;i++)
{
if(str1[i]==str2[n-1])
break;
}
root->left=creat(i,str1,str2)
root->right=creat(n-i-1,str1+i+1,str2+i);
return root;
}
int main()
{
int n;
char str1[1100],str2[1100];
int m;
scanf("%d",&m);
while(m--)
{
scanf("%s",str1);
scanf("%s",str2);
n=strlen(str1);
creat(n,str1,str2);
printf("\n");
}
return 0;
}
/***************************************************
User name: LAN7
Result: Accepted
Take time: 0ms
Take Memory: 104KB
Submit time: 2017-02-07 16:08:17
****************************************************/
整个代码和上一个差不多
root->data=str2[n-1];
printf("%c",root->data);
for(i=0;i<n;i++)
{
if(str1[i]==str2[n-1])
break;
}
root->left=creat(i,str1,str2)
root->right=creat(n-i-1,str1+i+1,str2+i);
就这里改动了一些数据,不过也非常容易理解,只要你能把后序遍历的熟练的掌握就好,str2 才是后序遍历,那么,最后一个肯定是根节点,这样就找到了,然后再递归建树,在后序遍历中,最后一个肯定是根节点,然后最前面的肯定是最左边的,同理,我们就可以把下一步的各个位置找到。成功建树。