Hdu 3791 Binary Search Tree, hdu1_1 Binary Search Tree
Original question link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? Id = 20911
Simple questions:
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 n Ull = 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 LOCAL63 freopen ("in.txt", "r", stdin); 64 freopen ("out.txt", "w +", stdout); 65 # endif66 int n; 67 while (~ Scanf ("% d", & 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