HDU 5444 Elven Postman (acm/icpc Asia regional Changchun Online)

Source: Internet
Author: User

"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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.