(二叉树创建+查找)精灵邮差
題目
精靈是非常奇特的生物。眾所周知,他們可以活很長時間,他們神奇的能力不是一件容易接受的事情。此外,他們住在樹上。但是,你可能不知道有些事情。雖然通過魔法傳送提供東西非常方便(很像電子郵件)。他們有時仍然喜歡其他更“傳統”的方法。
因此,作為一名精靈郵遞員,了解如何將郵件傳遞到樹的正確房間至關重要。精靈樹在交叉時總是分成不超過兩條路徑,無論是東方向還是西方。巧合地看起來非常像人類計算機科學家所知的二叉樹。不僅如此,在為房間編號時,他們總是將房間編號從最東邊的位置編號到西邊。東部的房間通常更優選,更昂貴,因為他們有幸看到日出,這在精靈文化中很重要。
無論如何,精靈們通常會在樹的根部按順序記下所有房間,以便郵遞員知道如何發送郵件。序列如下,它將直接訪問最東邊的房間并記下沿途遇到的每個房間。到達第一個房間后,它將進入下一個未訪問過的最東邊的房間,在路上寫下每個未訪問的房間,直到所有房間都被訪問。
您的任務是根據寫在根上的順序確定如何到達某個房間。
例如,序列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,向左子樹繼續找,找到就返回
總結
以上是生活随笔為你收集整理的(二叉树创建+查找)精灵邮差的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: adminer.php下载,Admine
 - 下一篇: java strcpy,详解C语言中st