日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(二叉树创建+查找)精灵邮差

發布時間:2025/3/12 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (二叉树创建+查找)精灵邮差 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

精靈是非常奇特的生物。眾所周知,他們可以活很長時間,他們神奇的能力不是一件容易接受的事情。此外,他們住在樹上。但是,你可能不知道有些事情。雖然通過魔法傳送提供東西非常方便(很像電子郵件)。他們有時仍然喜歡其他更“傳統”的方法。

因此,作為一名精靈郵遞員,了解如何將郵件傳遞到樹的正確房間至關重要。精靈樹在交叉時總是分成不超過兩條路徑,無論是東方向還是西方。巧合地看起來非常像人類計算機科學家所知的二叉樹。不僅如此,在為房間編號時,他們總是將房間編號從最東邊的位置編號到西邊。東部的房間通常更優選,更昂貴,因為他們有幸看到日出,這在精靈文化中很重要。

無論如何,精靈們通常會在樹的根部按順序記下所有房間,以便郵遞員知道如何發送郵件。序列如下,它將直接訪問最東邊的房間并記下沿途遇到的每個房間。到達第一個房間后,它將進入下一個未訪問過的最東邊的房間,在路上寫下每個未訪問的房間,直到所有房間都被訪問。

您的任務是根據寫在根上的順序確定如何到達某個房間。

例如,序列2,1,4,3將寫在下一棵樹的根上。
Input
First you are given an integer T(T≤10) indicating the number of test cases.

For each test case, there is a number n(n≤1000) on a line representing the number of rooms in this tree. n integers representing the sequence written at the root follow, respectively a1,…,an where a1,…,an∈{1,…,n}.

On the next line, there is a number q representing the number of mails to be sent. After that, there will be q integers x1,…,xq indicating the destination room number of each mail.
Output
For each query, output a sequence of move (E or W) the postman needs to make to deliver the mail. For that E means that the postman should move up the eastern branch and W the western one. If the destination is on the root, just output a blank line would suffice.

Note that for simplicity, we assume the postman always starts from the root regardless of the room he had just visited.
Sample Input
2
4
2 1 4 3
3
1 2 3
6
6 5 4 3 2 1
1
1
Sample Output
E

WE
EEEEE

分析與解答

1.由于輸入是先根,然后小的在e,大的在w按,按根左右建樹
左右根據數和根的大小判斷,大的我們建右子樹,小的左


利用每個子結點都可看作是樹根來建
2.查找的時候,x小與根的值,輸出e,向左子樹繼續找,找到就返回

#include<cstdio> #include<algorithm> using namespace std; struct node {int v;node *left,*right; }; node * insert(node *p,int x) {if(p==NULL){node * q=new node;q->v=x;q->left=NULL;q->right=NULL;return q;}else{if(x<p->v) p->left=insert(p->left,x);else p->right=insert(p->right,x);return p;} } void find (node *p,int x){if(p==NULL) return ;else if(x==p->v) return;else if(x<p->v) {printf("E");find(p->left,x);}else if(x>p->v){printf("W");find(p->right,x);} } void remove (node *p){if(p==NULL) return;remove(p->left);remove(p->right);free(p); }int main(){int t,n,m,x,y;scanf("%d",&t);while(t--){node *root =NULL;remove(root);scanf("%d",&n);for(int i=0;i<n;++i){scanf("%d",&x);root = insert(root,x);} scanf("%d",&m);for(int i=0;i<m;++i){scanf("%d",&y);find(root,y);printf("\n");}} } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的(二叉树创建+查找)精灵邮差的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。