zoukankan      html  css  js  c++  java
  • 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
    ");
            }
        }
    }
  • 相关阅读:
    hdu 4947
    hdu 4946
    hdu 4944
    hdu 4942
    hdu 4941
    PAT 【L2-011 玩转二叉树】
    PAT【L2-006 树的遍历】
    XYNUOJ 【2070: 重建二叉树】
    XYNUOJ 【1367: 二叉链表存储的二叉树】
    XYNUOJ 2390【二叉树遍历2】
  • 原文地址:https://www.cnblogs.com/slothrbk/p/9073954.html
Copyright © 2011-2022 走看看