团体程序设计天梯赛-练习集 L2-011. 玩转二叉树

来源:互联网 发布:信用卡怎么淘宝套现 编辑:程序博客网 时间:2024/04/30 17:55

团体程序设计天梯赛-练习集
L2-011. 玩转二叉树
https://www.patest.cn/contests/gplt/L2-011
和树的遍历那道题一样。

#include<iostream>#include<cstdio>using namespace std;int N, z;int qian[35], zhong[35], cen[35];void findc(int s, int d, int cnt) {    if (s > d) {        return;    }    if (z > N - 1) {        return;    }    for (int i = s; i <= d; i++) {        if (zhong[i] == qian[z]) {            cen[i] = cnt;            z++;            findc(s, i - 1, cnt + 1);            findc(i + 1, d, cnt + 1);            break;        }    }}int main() {    while (scanf("%d\n", &N) != EOF) {        for (int i = 0; i < N; i++) {            scanf("%d", &zhong[i]);        }        for (int i = 0; i < N; i++) {            scanf("%d", &qian[i]);        }        z = 0;        findc(0, N - 1, 0);        for (int i = 0, k = 0; k < N; i++) {            for (int j = N - 1; j >= 0; j--) {                if (cen[j] == i) {                    k++;                    printf(i ? " %d" : "%d", zhong[j]);                }            }        }        printf("\n");    }    return 0;}
0 0
原创粉丝点击