hdu3791 二叉搜索树

来源:互联网 发布:网络上赌博游戏原理 编辑:程序博客网 时间:2024/05/20 22:01

。。。。。。


没仔细看题交了7遍。。。


“一开始一个数n”。。。

反正我是当成就一组了。。。


今天写的水题就一道一A了。。。

没救了。。。


还有。。。别人的代码越来越看不惯了。。。_(:з」∠)_

【没回收内存是太懒。。。不要学我。。。


#include <iostream>#include <cstdio>#include <cstring>int n,i;char s[20];struct tree{tree* ch[2];int key;tree(){ch[0]=ch[1]=NULL;key=-1;}};void ins(tree* rt,int x){int k=rt->key<x;while (rt->ch[k]!=NULL){rt=rt->ch[k];k=rt->key<x;}rt->ch[k]=new tree();rt->ch[k]->key=x;}int check(tree* rt1,tree* rt2){if (rt1==NULL&&rt2==NULL) return 1;if (rt1==NULL||rt2==NULL) return 0;if (rt1->key!=rt2->key) return 0;return check(rt1->ch[0],rt2->ch[0])&&check(rt1->ch[1],rt2->ch[1]);}int main(){while (scanf("%d",&n),n){scanf("%s",s);tree* rt=new tree();rt->key=s[0]-'0';for (i=1;s[i];i++) ins(rt,s[i]-'0');while (n--){scanf("%s",s);tree* rt2=new tree();rt2->key=s[0]-'0';for (i=1;s[i];i++) ins(rt2,s[i]-'0');if (check(rt,rt2)) puts("YES"); else puts("NO");free(rt2);}}return 0;}


0 0
原创粉丝点击