PAT 甲級 刷題日記|A 1123 Is It a Complete AVL Tree (30 分)

單詞

complete binary tree 完全二叉樹

restore 修復 恢復

題目

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
1628848464776.png

Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL 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. Then in the next line, print YES if the tree is complete, or NO if not.

Sample Input 1:

5
88 70 61 63 65
結(jié)尾無空行

Sample Output 1:

70 63 88 61 65
YES
結(jié)尾無空行

Sample Input 2:

8
88 70 61 96 120 90 65 68
結(jié)尾無空行

Sample Output 2:

88 65 96 61 70 90 120 68
NO
結(jié)尾無空行

思路

平衡二叉樹的題目模板固定艘希。

判斷一個樹是否為完全二叉樹祭刚,層次遍歷,若出現(xiàn)空節(jié)點后又出現(xiàn)了節(jié)點,則不是完全二叉樹旬盯。

代碼

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

vector<int> ans;
int flag = 1;
int after = 0;

struct node{
    int v, height;
    node *lchild, *rchild;
    node(int data): v(data), height(1), lchild(NULL), rchild(NULL){
    }
};

int getHeight(node* root) {
    if (root == NULL) return 0;
    return root->height;
}

void updateHeight(node* root) {
    root->height = max(getHeight(root->lchild), getHeight(root->rchild)) + 1;
}

int getFactor(node* root) {
    return getHeight(root->lchild) - getHeight(root->rchild);
}

void L(node* &root) {
    node* temp = root->rchild;
    root->rchild = temp->lchild;
    temp->lchild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}

void R(node* &root) {
    node* temp = root->lchild;
    root->lchild = temp->rchild;
    temp->rchild = root;
    updateHeight(root);
    updateHeight(temp);
    root = temp;
}

void insert(node* &root, int v) {
    if (root == NULL) {
        root = new node(v);
        return;
    }
    if (v < root->v) {
        insert(root->lchild, v);
        updateHeight(root);
        if (getFactor(root) == 2) {
            if (getFactor(root->lchild) == 1) {
                R(root);
            } else if(getFactor(root->lchild) == -1) {
                L(root->lchild);
                R(root);
            }
        }
    } else {
        insert(root->rchild, v);
        updateHeight(root);
        if (getFactor(root) == -2) {
            if (getFactor(root->rchild) == 1) {
                R(root->rchild);
                L(root);
            } else if(getFactor(root->rchild) == -1) {
                L(root);
            
            }
        }
    }
}

void level(node *root) {
    if (root == NULL) {
        flag = 2;
        return;
    }
    queue<node*> mq;
    mq.push(root);
    while (!mq.empty()) {
        node* now = mq.front();
        ans.push_back(now->v);
        mq.pop();
        if (flag == 1) {
            if (now->lchild != NULL || now->rchild != NULL) flag = 2;
        }
        if (now->lchild != NULL) {
            if (after) flag = 0;
            mq.push(now->lchild);
        } else {
            after = 1;
        }
        if (now->rchild != NULL) {
            if (after) flag = 0;
            mq.push(now->rchild);
        } else {
            after = 1;
        }
        
        
    }
}


int main() {
    int n;
    int data[10000];
    cin>>n;
    node* root = NULL;
    for (int i = 0; i < n; i++) {
        cin>>data[i];
        insert(root, data[i]);
    }
    level(root);
    for (int i = 0; i < n; i++) {
        cout<<ans[i];
        if (i != n - 1) cout<<" ";
        else cout<<endl;
    }
    
    if (flag == 0) cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
    
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末批幌,一起剝皮案震驚了整個濱河市经伙,隨后出現(xiàn)的幾起案子扶叉,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件辜梳,死亡現(xiàn)場離奇詭異粱甫,居然都是意外死亡泳叠,警方通過查閱死者的電腦和手機作瞄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來危纫,“玉大人宗挥,你說我怎么就攤上這事∑豕ⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵螃征,是天一觀的道長。 經(jīng)常有香客問我盯滚,道長,這世上最難降的妖魔是什么魄藕? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮背率,結(jié)果婚禮上话瞧,老公的妹妹穿的比我還像新娘。我一直安慰自己寝姿,他們只是感情好交排,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著饵筑,像睡著了一般个粱。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上翻翩,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天都许,我揣著相機與錄音,去河邊找鬼嫂冻。 笑死胶征,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的桨仿。 我是一名探鬼主播睛低,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了钱雷?” 一聲冷哼從身側(cè)響起骂铁,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎罩抗,沒想到半個月后拉庵,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡套蒂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年钞支,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片操刀。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡烁挟,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出骨坑,到底是詐尸還是另有隱情撼嗓,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布欢唾,位于F島的核電站且警,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏匈辱。R本人自食惡果不足惜振湾,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望亡脸。 院中可真熱鬧押搪,春花似錦、人聲如沸浅碾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽垂谢。三九已至厦画,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間滥朱,已是汗流浹背根暑。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留徙邻,地道東北人。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓淳地,卻偏偏與公主長得像怖糊,于是被迫代替她去往敵國和親伍伤。 傳聞我的和親對象是個殘疾皇子遣钳,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353

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