Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5444
GF Changchun Online Game did the problem, bare two forks to find the tree. Drop the data into a tree to find the value, go to a left son output an E, go to a right son output a W, walk to the end of the output.
Code:
1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> + - using namespacestd; + AtypedefstructNode { atnode*Left ; -node*Right ; - intdata; -Node () {left = NULL;NULL;} - }node; - innode* Insert (node* cur,intdata) { - if(cur = =NULL) { toCur =NewNode (); +Cur->data =data; - returncur; the } * if(Data > Cur->data) { $Cur->right = insert (cur->Right , data);Panax Notoginseng } - Else { theCur->left = insert (cur->Left , data); + } A returncur; the } + - voidSolve (node* cur,intx) { $ if(cur = = NULL | | Cur->data = =x) { $printf"\ n"); - return ; - } the - if(X > Cur->data) {Wuyiprintf"W"); the returnSolve (cur->Right , x); - } Wu if(X < cur->data) { -printf"E"); About returnSolve (cur->Left , x); $ } - } - - intN, q, x, TMP; A + intMain () { the //freopen ("Input", "R", stdin); - intT; $scanf"%d", &T); the while(t--) { thescanf"%d", &n); thenode* root =NULL; the for(inti =0; I < n; i++) { -scanf"%d", &tmp); inRoot =Insert (root, tmp); the } thescanf"%d", &q); About while(q--) { thescanf"%d", &x); the solve (root, x); the } + } - return 0; the}
[HDOJ5444] Elven Postman