程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 3791 二叉搜索樹,hdu3791二叉

hdu 3791 二叉搜索樹,hdu3791二叉

編輯:C++入門知識

hdu 3791 二叉搜索樹,hdu3791二叉


原題鏈接: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

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved