首页 > 代码库 > 题目1078:二叉树遍历(由前序遍历中序遍历得到后序遍历)
题目1078:二叉树遍历(由前序遍历中序遍历得到后序遍历)
题目链接:http://ac.jobdu.com/problem.php?pid=1078
题目详解:https://github.com/zpfbuaa/JobduInCPlusPlus
//// 1078 二叉树遍历.cpp// Jobdu//// Created by PengFei_Zheng on 09/04/2017.// Copyright © 2017 PengFei_Zheng. All rights reserved.// #include <stdio.h>#include <iostream>#include <algorithm>#include <string.h> using namespace std; struct Node{ Node *lchild; Node *rchild; char c;}tree[50]; int loc;Node *creat(){ tree[loc].lchild=tree[loc].rchild=NULL; return &tree[loc++];} char str1[30], str2[30];void postOrder(Node *T){ if(T->lchild!=NULL) postOrder(T->lchild); if(T->rchild!=NULL) postOrder(T->rchild); printf("%c",T->c);} Node *build(int s1, int e1, int s2, int e2){ Node* ret = creat(); ret->c=str1[s1]; int rootIdx=0; for(int i = s2 ; i <=e2 ; i++){ if(str2[i] == str1[s1]){ rootIdx=i; break; } } if(rootIdx!=s2){ ret->lchild=build(s1+1, s1+(rootIdx-s2), s2, rootIdx-1); } if(rootIdx!=e2){ ret->rchild=build(s1+(rootIdx-s2)+1, e1, rootIdx+1, e2); } return ret;} int main(){ while(scanf("%s",str1)!=EOF){ scanf("%s",str2); loc=0; int l1 = (int)strlen(str1); int l2 = (int)strlen(str2); Node* T = build(0,l1-1,0,l2-1); postOrder(T); printf("\n"); } return 0;} /************************************************************** Problem: 1078 User: zpfbuaa Language: C++ Result: Accepted Time:0 ms Memory:1520 kb****************************************************************/
题目1078:二叉树遍历(由前序遍历中序遍历得到后序遍历)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。