hdoj-3791-二叉搜索树(二叉搜索树模板题)

#include <cstring>
#include <cstdio>
#include <iostream>
using namespace std;

typedef int ElemType;
template<typename T>
int getArrayLength(T &array) {
    return (sizeof(array) / sizeof(array[0]));
}

typedef struct node{
    ElemType data;
    struct node *lchild, *rchild;
}*BST;

bool insertBST(BST &T, ElemType element) {
    if (T == NULL) {
        T = new node;
        T->data = element;
        T->lchild = T->rchild = NULL;
        return true;
    }
    if (T->data == element) return false;
    if (element< T->data) insertBST(T->lchild, element);
    else insertBST(T->rchild, element);
}
//judge
int flag;
void judge(BST T1, BST T2) {
    if (T1 == NULL && T2 == NULL) {
        return ;
    } else if(T1->data != T2->data) {//数据比较坑 
        flag = 0;
        return ;
    } else if(((T1->lchild !=NULL && T2->lchild !=NULL) || (T1->lchild ==NULL && T2->lchild ==NULL)) && 
        ((T1->rchild !=NULL && T2->rchild !=NULL) || (T1->rchild ==NULL && T2->rchild ==NULL))) {//需要考虑全面 
        judge(T1->lchild, T2->lchild);
        judge(T1->rchild, T2->rchild);
    }else flag = 0;
}
int main() {
    int t, n ,k ,x;
    BST tree[25];
    
    while (scanf("%d", &t)!=EOF && t) {
        memset(tree, 0, sizeof(tree));
        for (int i=0; i<t+1; i++) {
            BST T = NULL;
            char s[11];
            scanf("%s", s);
            for (int j=0; j<=strlen(s)-1; j++) 
                insertBST(T, s[j]-'0');
            tree[i] = T;
        }
        for (int i=1; i<t+1; i++) {
            flag = 1;
            judge(tree[0], tree[i]);
            if (flag) printf("YES
");
            else printf("NO
");
        }
    }
}
原文地址:https://www.cnblogs.com/langyao/p/9073954.html