"Topic link": Click here~~
"To the effect of the topic":
HDU 5444
Test instructions: The number of n is constantly inserted in the initial empty two-fork tree. For each number, starting from the root node, if the current node is empty, insert the current node, if the current node is not empty, is less than the value of the current node, insert the right subtree, otherwise insert the left sub-tree.
Then q, each time a value is queried for a lookup path starting from the root node in the binary tree.
3
Simulate the entire insertion and questioning process directly with a binary tree
Code:
/** problem:hdu no.5444* Running time:0ms* complier:g++* author:javaherongwei* Create time:12:15 2015/9/16 Wed * Binar Y search tree*/#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> Using namespace Std;typedef long long ll;struct BST//binary serach tree{int data; BST * LEFTCHILD; Lson BST * rightchild; Rson BST () {} BST (int x) {//init value data=x; Leftchild=rightchild=null; }};void Build (BST *&root,int key)//creat the tree{if (key<root->data) {if (null==root->leftchild) { root->leftchild= new BST (key); Return } else build (Root->leftchild,key); } else{if (null==root->rightchild) {root->rightchild= new BST (key); Return } else build (Root->rightchild,key); }}void Get_path (BST *root,int key)//query{if (Root->data==key) {puts (""); return; } else if (key<root->data) {//Lson Putchar (' E '); Get_path (Root->leftchild,key); } else{Putchar (' W '); Rson Get_path (Root->rightchild,key); }}int Main () {int t,rt,x,n,m; scanf ("%d", &t); while (t--) {scanf ("%d", &n); scanf ("%d", &rt); BST *root= new BST (RT); for (int i=1; i<n; ++i) {scanf ("%d", &x); Build (ROOT,X); } scanf ("%d", &m); int q; while (m--) {scanf ("%d", &q); Get_path (ROOT,Q); } root=null; } return 0;} /*sample Input242 1 4 331 2 366 5 4 3 2 111Sample outputeweeeeee*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5444 Elven Postman (acm/icpc Asia regional Changchun Online)