zoukankan      html  css  js  c++  java
  • 1110 Complete Binary Tree (25分)

    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 (≤) which is the total number of nodes in the tree -- and hence the nodes are numbered from 0 to N1. 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:

    9
    7 8
    - -
    - -
    - -
    0 1
    2 3
    4 5
    - -
    - -
    
     

    Sample Output 1:

    YES 8
    
     

    Sample Input 2:

    8
    - -
    4 5
    0 6
    - -
    2 3
    - 7
    - -
    - -
    
     

    Sample Output 2:

    NO 1

    完全二叉树的判定,我们可以通过层序遍历,看看有无空缺即可,如果是完全二叉树,打印层序最后一个节点,否则打印第一个。

    #include <iostream>
    #include <vector>
    #include <queue>
    using namespace std;
    struct node {
        int v;
        string left, right;
    }n[30];
    int N, root = 0, last;
    bool isComplete(int root) {
        queue<int> que;
        que.push(root);
        bool flag = 1, ret = true;
        while(!que.empty()) {
            node tmp = n[que.front()];
            que.pop();
            last = tmp.v;
            if(tmp.left != "-") que.push(stoi(tmp.left));
            if(tmp.right != "-") que.push(stoi(tmp.right));
            if(flag) {
                if(tmp.left != "-" && tmp.right != "-") {
                } else if(tmp.left != "-") {
                    flag = 0;
                } else if(tmp.right != "-") {
                    ret = false;
                } else {
                    flag = 0;
                }
            } else {
                if(tmp.left != "-" || tmp.right != "-") ret = false;
            }
        }
        return ret;
    }
    int main() {
        cin >> N;
        vector<int> find_root(N, 0);
        for(int i = 0; i < N; i++) {
            n[i].v = i;
            cin >> n[i].left >> n[i].right;
            if(n[i].left != "-") find_root[stoi(n[i].left)] = 1;
            if(n[i].right != "-") find_root[stoi(n[i].right)] = 1;
        }
        while(root < N && find_root[root]) root++;
        bool complete = isComplete(root);
        printf("%s %d
    ", complete ? "YES": "NO", complete ? last: root);
        return 0;
    }
  • 相关阅读:
    LightOJ 1245(Harmonic Number (II))
    牛客练习赛13 乌龟跑步(DP)
    vue-cli 打包编译 -webkit-box-orient: vertical 被删除解决办法
    vue静态文件处理
    vue项目关闭eslint检查
    Mac 桌面软件开发基础问答
    Mac App开发
    mac os app 开发
    vue中html模板使用绑定的全局函数
    软件版本标识
  • 原文地址:https://www.cnblogs.com/littlepage/p/12868521.html
Copyright © 2011-2022 走看看