已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
2017-02-22 21:03
483 查看
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { int data; struct node *lchild,*rchild; }; struct node *creat(int n,char *a,char *b) { struct node *root; char *p; if(n == 0) return NULL; root = (struct node*)malloc(sizeof(struct node)); root -> data = a[n - 1]; for(p = b; p != '\0'; p++) if(*p == a[n - 1]) break; int t = p - b; root -> lchild = creat(t, a, b); root -> rchild = creat(n - t - 1, a + t, p + 1); return root; } void xianxu(struct node *root) { if(root) { printf("%c",root->data); xianxu(root->lchild); xianxu(root->rchild); } } int main() { int m, t; scanf("%d", &t); getchar(); while(t--) { struct node *root; root=(struct node *)malloc(sizeof(struct node)); char a[100],b[100]; scanf("%s %s",b,a); m=strlen(a); root=creat(m,a,b); xianxu(root); printf("\n"); } }
相关文章推荐
- 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历
- 已知二叉树先序遍历中序遍历求后序遍历
- 已知一颗二叉树,如果先序遍历的节点顺序是:ADCEFGHB,中序遍历是,CDFEGHAB,则后序遍历的结果为
- 已知二叉树先序遍历,中序遍历创建二叉树并输出后序遍历
- 已知二叉树的中序遍历和前序遍历,如何求后序遍历
- [leetcode]_根据二叉树的先序遍历(后序遍历) + 中序遍历 重建二叉树
- 已知二叉树的中序遍历和前序遍历,如何求后序遍历
- 二叉树系列(1)已知二叉树的中序遍历和前序遍历,如何求后序遍历
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)及运行实例结果
- 二叉树先序遍历,中序遍历和后序遍历的非递归实现
- 二叉树系列:已知二叉树的中序遍历和前序遍历,求后序遍历
- 实现二叉树的先序遍历、中序遍历、后序遍历的递归非递归算法以及层次遍历算法
- 已知二叉树的中序遍历和后序遍历,求先序遍历
- c++实现二叉树的先序遍历,中序遍历,后序遍历(递归方法)及运行实例结果
- 二叉树的先序遍历、中序遍历、后序遍历、层次遍历的递归实现
- 二叉树先序遍历中序遍历建立二叉树然后后序遍历
- 二叉树中的先序遍历、中序遍历和后序遍历关系
- java实现二叉树的先序遍历,中序遍历,后序遍历
- 组合模式实现二叉树先序遍历,中序遍历和后序遍历
- 二叉树先序遍历、中序遍历、后序遍历