标签:des style blog http color os strong io
Description
Input
Output
Sample Input
Sample Output
#include <stdio.h> #include <string.h> int n; int a[1000], b[1000], c[1000]; void build( int len, int s1, int s2, int s ) { int p; int i; if(len<=0) return; else { for(i=s2; i<s2+len; i++ ) { if(b[i]==a[s1]) { p = i; break; } } c[s+len-1] = a[s1]; build(p, s1+1, s2, s ); //左 build(len-p-1, s1+p+1, s2+p+1, s+p); //右 } } int main() { int i, j; while(scanf("%d", &n)!=EOF) { for(i=0; i<n; i++) { scanf("%d", &a[i] ); } for(i=0; i<n; i++) { scanf("%d", &b[i] ); } build(n, 0, 0, 0); for(i=0; i<n; i++) { printf("*%d ", c[i] ); } } return 0; }
#include <stdio.h> #include <string.h> void build(int len, int *s1, int *s2, int *s) { int p; int i; if(len<=0) return; else { for(i=0; i<len; i++) { if(s2[i]==s1[0]) { p = i; } } build(p, s1+1, s2, s); build(len-p-1, s1+p+1, s2+p+1, s+p); s[len-1] = s1[0]; } } int main() { int i; int s1[1000], s2[1000], s3[1000]; int n; while(scanf("%d", &n)!=EOF) { for(i=0; i<n; i++) { scanf("%d", &s1[i] ); } for(i=0; i<n; i++) { scanf("%d", &s2[i] ); } build(n, s1, s2, s3 ); for(i=0; i<n; i++) { printf("%d%c", s3[i], i==n-1?‘\n‘:‘ ‘ ); } } return 0; }
hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序),布布扣,bubuko.com
hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)
标签:des style blog http color os strong io
原文地址:http://www.cnblogs.com/yspworld/p/3881534.html