新手对二叉树先序,中序,后序的学习(POJ - 2255 (递归))
Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
This is an example of one of her creations:
D / \ / \ B E / \ \ / \ \ A C G / / F
To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).
Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
However, doing the reconstruction by hand, soon turned out to be tedious.
So now she asks you to write a program that does the job for her!
Input
The input will contain one or more test cases.
Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
Input is terminated by end of file.
Output
For each test case, recover Valentine’s binary tree and print one line containing the tree’s postorder traversal (left subtree, right subtree, root).
Sample Input
DBACEGF ABCDEFG
BCAD CBAD
Sample Output
ACBFGED
CDAB
此图 先序为 根左右 也就是 1 左(根左右 2 4 5)右(根 左右 3 6 7)
即 先序 1 2 4 5 3 6 7
后序 为 左右根 左(左 右 根 4 5 2)右(左 右 根 6 7 3)根 1
中序 左 根 右 左(4 2 5) 根 1 右(6 3 7)
出题有 给出中序 后序 求先序 ,给出 中序 先序 求 后序。
本题 为求后序
算法知识:递归
跟着流程 给出 先序 DBACEGF 中序 ABCDEFG 那么根据先序 先根 为 D 又 中序为左根右 那么 左边为ABC 右边为EFG 那么 树为
那么后序为左右根 即为
ACBGFED
#include<stdio.h> #include<string.h> using namespace std; char a[1002],b[1002],flag; void dfs(int l1,int r1,int l2,int r2) { if(l1>r1) return ; int i,k=-1; for(i=l2;a[i]!=b[l1];i++); dfs(l1+1,r1-r2+i,l2,i-1); dfs(r1-r2+i+1,r1,i+1,r2); printf("%c",b[l1]); } int main() { int n,i; while(~scanf("%s",&b)) { int l1,l2; scanf("%s",&a); l1=strlen(b); l2=strlen(a); dfs(0,l1-1,0,l2-1); printf("\n"); } return 0; }
/**p=strchr(s2,s1[0]); *p里边存的是在s2中第一次出现s1[0]的位置开始一直到s2字符串结束, p=strchr(s2,s1[0])-s2; p里边存的是s1[0]在s2中的位置,是一个数。& DBACEGF ABCDEFG BCAD CBAD 样例输出 : ACBFGED CDAB*/ #include <stdio.h> #include <string.h> char s1[20],s2[20],ans[20]; void build(int n,char* s1,char*s2,char *s) { if(n<=0) return ; int p=strchr(s2,s1[0])-s2; //找到根结点在s2中的位置 build(p,s1+1,s2,s); //建立左子树的后序遍历 build(n-p-1,s1+p+1,s2+p+1,s+p);//建立右子树的后序遍历 s[n-1]=s1[0];//在最后的位置将根结点插入 }//整个遍历之后,一次递归将当前的根结点放在最后,递归完毕,后序遍历也就出来了。 int main (void) { int n,i; while(scanf("%s%s",s1,s2)!=EOF) //s1表示先序遍历,s2代表中序遍历 { n=strlen(s1); build(n,s1,s2,ans); //构造后序遍历 ans[n]='\0'; printf("%s\n",ans); //输出后序遍历 } return 0; }
#include <iostream> #include <cstring> #include <cstdlib> using namespace std; typedef struct node { char ch; struct node *left, *right; }node; //定义节点的结构 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); cout << head -> ch; } int main() { char pre[30], in[30]; //存储先序和中序遍历的序列 node *head; head = (node*)malloc(sizeof(node)); while (cin >> pre >> in){ int len = strlen(pre); head = creat(pre, in, len); print(head); cout << endl; } return 0; }Starry_Sky_Dream 原创文章 50获赞 4访问量 2368 关注 私信
- c++学习之前序递归遍历二叉树和中序循环遍历二叉树
- poj2255 二叉树给出先序中序求后序序列
- 树的学习——(递归构建二叉树、递归非递归前序中序后序遍历二叉树、根据前序序列、中序序列构建二叉树)
- 【Java语言学习】之递归、非递归前序、中序、后续遍历二叉树
- 树的学习——(递归构建二叉树、递归非递归前序中序后序遍历二叉树、根据前序序列、中序序列构建二叉树)
- c++学习之前序递归遍历二叉树和中序循环遍历二叉树
- POJ 2255 根据二叉树的前序和中序序列来重建二叉树
- poj2255 根据二叉树的前序和中序遍历 求出树的后序遍历
- ACM--二叉树-已知前中序求后序--POJ-2255
- poj2255 根据任意二叉树的先序遍历和中序遍历求解后序遍历
- 某国内IT大牌名企校招笔试 + 树的学习—(递归构建二叉树、递归非递归前序中序后序遍历二叉树、根据前序序列、中序序列构建二叉树)
- 二叉树的先序创建,先序,中序,后序的递归与非递归遍历,层次遍历,叶子结点数及树的深度
- 二叉树前序、中序、后续遍历(递归实现)
- 二叉树前序、中序、后序遍历非递归写法的透彻解析
- 二叉树的先序、中序、后序、层序递归及非递归遍历
- 二叉树的前序、中序、后续查找(非递归!)
- 二叉树的前序,中序,后序的递归与非递归遍历以及按层遍历
- JAVA 先序、中序、后序、层序,递归非递归遍历二叉树
- Python 实现二叉树的前序、中序、后序、层次遍历(递归和非递归版本)
- C丨二叉树的遍历丨前序、中序、后序的递归和非递归实现+层次遍历