文章

1

粉丝

153

获赞

1

访问

823

头像
二叉搜索树 题解:
P1317 浙江大学机试题
发布于2023年5月31日 17:28
阅读数 823

不用建树的做法,直接判断两个序列每次分开的左右子树不为空的话,其左右子树第一位是否相等即可 。

#include <iostream>
#include <vector>
using namespace std;
#define SIZE 10

int judge(int* a, int x, int* b, int y);

int main(void)
{
    int N;
    int bst[SIZE], tgt[SIZE];
    while(cin>>N){
        if (N == 0)
            break;
        string a;
        cin >> a;
        int len = a.size();
        for (int i = 0; i < len; i++) {
            bst[i] = a[i] - '0';
        }
        for (int i = 0; i < N; i++) {
            string b;
            cin >> b;
            for (int j = 0; j < len; j++) {
                tgt[j] = b[j] - '0';
            }
            int n = a.size();
            if (judge(bst, n, tgt, n))
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}

int judge(int* a, int x, int* b, int y)
{
    if (x != y)
        return 0;
    if (x == 0 && y == 0) {
        re...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发