PAT 甲級 刷題日記|A 1064 Complete Binary Search Tree (30 分)

單詞積累

complete 完整的 完全的

exception 例外 異議

題目

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input:

10
1 2 3 4 5 6 7 8 9 0
結(jié)尾無空行

Sample Output:

6 3 8 1 5 7 9 0 2 4
結(jié)尾無空行

思路

利用完全二叉排序樹的性質(zhì),找到根節(jié)點 左節(jié)點 右節(jié)點汇恤,來完成建樹

建樹完成后,利用層次遍歷即可得到最終結(jié)果

看了別人的題解之后惨寿,感覺自己處理地麻煩太多了槐秧。更優(yōu)的思路是利用排序贤壁,得到二叉樹的中序序列(二叉排序樹的性質(zhì):中序遍歷結(jié)果就是數(shù)字的升序序列)疏遏,然后再通過中序序列建樹提揍,樹的存儲序列即為層次序列的值(完全二叉樹的性質(zhì):從1開始編號的完全二叉樹溉旋,i節(jié)點的左孩子編號為2i畸冲,右孩子編號為2i+1),非常巧妙

代碼1

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1000 + 2;
int num[maxn];
vector<int> res;

struct node{
    int data;
    node* leftchild;
    node* rightchild;
    node(int d): data(d), leftchild(NULL), rightchild(NULL){
    } 
};

node* create(int a, int b) {
    if (a == b) {
        node* anode = new node(num[a]);
        return anode;
    }
    if (a > b) return NULL; 
    int all = b - a + 1;  // 處理的節(jié)點數(shù) 
    int height = floor(log(all + 1) / log(2));  // 滿層的高度 
    int now = pow(2, height) - 1;  // 滿層的節(jié)點數(shù) 
    int left = (now - 1) / 2;
    int right = left;
    left += a;
    int next = pow(2, height);  //下一層的節(jié)點數(shù) 
    if ((all - now) <= next / 2) left += (all - now);
    else {
        left += next / 2;
        right += all - now - next/2;
    }
    node* root = new node(num[left]);
    root->leftchild = create(a, left - 1);
    root->rightchild = create(left + 1, b);
    return root;
}

void level(node* root) {
    queue<node*> mq;
    mq.push(root);
    while(!mq.empty()) {
        node* now = mq.front();
        mq.pop();
        res.push_back(now->data);
        if(now->leftchild != NULL) mq.push(now->leftchild);
        if(now->rightchild != NULL) mq.push(now->rightchild);
    }
    return;
}

int main() {
    int len;
    cin>>len;
    for (int i = 0; i< len; i++) {
        cin>>num[i];
    }
    sort(num, num+len);
    node* root = create(0,len-1);
    level(root);
    for (int i = 0; i < len; i++) {
        cout<<res[i];
        if (i != len - 1) cout<<" ";
    }
} 

代碼2

#include <bits/stdc++.h>
using namespace std;

const int maxn = 10000;
int len;
int index = 0;
int num[maxn];
int res[maxn];

void Inorder(int root) {
    if (root > len) return;
    Inorder(root * 2);
    res[root] = num[index++];
    Inorder(root * 2 + 1);  
}

int main() {
    cin>>len;
    for (int i = 0; i < len; i++) {
        cin>>num[i];
    }
    sort(num, num + len);
    Inorder(1);
    for (int i = 1; i <= len; i++) {
        cout<<res[i];
        if (i != len) cout<<" ";
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末观腊,一起剝皮案震驚了整個濱河市邑闲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌梧油,老刑警劉巖苫耸,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異儡陨,居然都是意外死亡褪子,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門骗村,熙熙樓的掌柜王于貴愁眉苦臉地迎上來嫌褪,“玉大人,你說我怎么就攤上這事胚股×矗” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵琅拌,是天一觀的道長缨伊。 經(jīng)常有香客問我,道長进宝,這世上最難降的妖魔是什么刻坊? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮即彪,結(jié)果婚禮上紧唱,老公的妹妹穿的比我還像新娘。我一直安慰自己隶校,他們只是感情好漏益,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著深胳,像睡著了一般绰疤。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上舞终,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天轻庆,我揣著相機與錄音癣猾,去河邊找鬼。 笑死余爆,一個胖子當(dāng)著我的面吹牛纷宇,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蛾方,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼像捶,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了桩砰?” 一聲冷哼從身側(cè)響起拓春,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎亚隅,沒想到半個月后硼莽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡煮纵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年懂鸵,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片行疏。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡矾瑰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出隘擎,到底是詐尸還是另有隱情,我是刑警寧澤凉夯,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布货葬,位于F島的核電站,受9級特大地震影響劲够,放射性物質(zhì)發(fā)生泄漏震桶。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一征绎、第九天 我趴在偏房一處隱蔽的房頂上張望蹲姐。 院中可真熱鬧,春花似錦人柿、人聲如沸柴墩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽江咳。三九已至,卻和暖如春哥放,著一層夾襖步出監(jiān)牢的瞬間歼指,已是汗流浹背爹土。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留踩身,地道東北人胀茵。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像挟阻,于是被迫代替她去往敵國和親琼娘。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容