PAT-A: 1110. Complete Binary Tree

来源:互联网 发布:鼹鼠网络 编辑:程序博客网 时间:2024/05/13 16:24

题目:

Given a tree, you are supposed to tell if it is a complete binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=20) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N-1. Then N lines follow, each corresponds to a node, and gives the indices of the left and right children of the node. If the child does not exist, a "-" will be put at the position. Any pair of children are separated by a space.

Output Specification:

For each case, print in one line "YES" and the index of the last node if the tree is a complete binary tree, or "NO" and the index of the root if not. There must be exactly one space separating the word and the number.

Sample Input 1:
97 8- -- -- -0 12 34 5- -- -
Sample Output 1:
YES 8
Sample Input 2:
8- -4 50 6- -2 3- 7- -- -
Sample Output 2:
NO 1
原文链接


#include <iostream>#include <vector>#include <cmath>        //pow()#include <cstdlib>      //atoi()#include <string>using namespace std;struct bt_vec{    int p;    int lc;    int rc;    int height;    bt_vec():p(-1),height(-1),lc(-1),rc(-1) {}};vector<bt_vec> t;int n(0);int root(0);void up_h(int r){    if(t[r].p >= 0) t[r].height = t[t[r].p].height + 1;    if(t[r].lc >= 0) up_h(t[r].lc);    if(t[r].rc >= 0) up_h(t[r].rc);}void update_height(){    t[root].height = 1;    up_h(root);}int main(){    cin>>n;    int i,judge(-1);    string c;        //因为节点数有可能会>=10,因此用string储存变量【易错点,用了char c】    t.resize(n+1);    for(i=0;i<n;i++){        cin>>c;        if(c!="-") {            t[i].lc = atoi(c.c_str());     // 变量名.c_str() ,转化string为 char*            t[t[i].lc].p = i;        }        cin>>c;        if(c!="-") {            t[i].rc = atoi(c.c_str());            t[t[i].rc].p = i;        }    }    for(i=1;i<n;i++){        if(t[i].p== -1){            root = i;            break;        }    }    update_height();    int n_hmax,h_max;      // n_hmax 底层节点数 ; h_max 底层高度;    for(i=0, n_hmax=0, h_max=-10; i<n; i++){   //寻找最底层节点,并存到fc里;        if(t[i].height > h_max){            h_max = t[i].height;            n_hmax = 1;        }else if(t[i].height == h_max){            n_hmax ++;        }    }    vector<int> lt(n);    int j(0);    lt[j++] = root;    for(i=0; i<j; i++){              //对t进行层序遍历        if(t[lt[i]].lc >= 0) lt[j++] = t[lt[i]].lc;        if(t[lt[i]].rc >= 0) lt[j++] = t[lt[i]].rc;    }    if( n_hmax + pow<int,int>(2,h_max-1) - 1 != n) judge = 0;  //完全二叉树总节点数 = 底层节点数 + 总层数-1的满二叉树总节点数    else {        int last_p = t[lt.back()].p;        for(i=0;i<n;i++){            if(t[ lt[i] ].height == h_max - 1) break;        }        for(j=i;j<n;j++){            if( lt[j] == last_p) break;        }        if( t [last_p].lc == lt.back() ){              //last_p为最后一个节点的父节点            if( n_hmax == (j-i)*2 + 1) judge = 1;      //完全二叉树最后一层的节点数 =        }else if( n_hmax == (j-i)*2 + 2) judge = 1;    //(层序遍历里last_p的秩-倒数第二层第一个节点的秩)*2 + last_p的子节点数        else judge = 0;    }    if(judge != 0) cout<<"YES "<<lt.back();    else cout<<"NO "<<root;    return 0;}


0 0
原创粉丝点击