hdu 3791 二叉搜索树

原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20911

简单题如下:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<vector>
 7 using std::vector;
 8 const int Max_N = 100;
 9 struct Node{
10     int v;
11     Node *ch[2];
12     inline void set(int _v = 0, Node *p = NULL){
13         v = _v;
14         ch[0] = ch[1] = p;
15     }
16 };
17 struct BinaryTree{
18     Node *tail, *root, *null;
19     Node stack[Max_N];
20     void init(){
21         tail = &stack[0];
22         null = tail++;
23         null->set();
24         root = null;
25     }
26     inline  Node *newNode(int v){
27         Node *p = tail++;
28         p->set(v, null);
29         return p;
30     }
31     inline void insert(Node* &x, int v){
32         if (x == null){
33             x = newNode(v);
34             return;
35         }
36         insert(x->ch[v > x->v], v);
37     }
38     inline void dfs(Node *x, vector<int> &ans){
39         if (x != null){
40             ans.push_back(x->v);
41             dfs(x->ch[0], ans);
42             dfs(x->ch[1], ans);
43         }
44     }
45     inline void insert(int v){
46         insert(root, v);
47     }
48     inline void dfs(vector<int> &ans){
49         dfs(root, ans);
50     }
51     inline void gogo(vector<int> &ans){
52         int m;
53         char buf[100]; 
54         while (getchar() != '\n');
55         scanf("%s", buf);
56         init(), m = strlen(buf);
57         for (int i = 0; i < m; i++) insert(buf[i] - '0');
58         dfs(ans);
59     }
60 }tree;
61 int main(){
62 #ifdef LOCAL
63     freopen("in.txt", "r", stdin);
64     freopen("out.txt", "w+", stdout);
65 #endif
66     int n;
67     while (~scanf("%d", &n) && n){
68         vector<int> a, b;
69         tree.gogo(a);
70         for (int i = 0; i < n; i++){
71             tree.gogo(b);
72             if (a == b) puts("YES");
73             else puts("NO");
74             b.clear();
75         }
76     }
77     return 0;
78 }
View Code

 

转载于:https://www.cnblogs.com/GadyPu/p/4479633.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值