zoukankan      html  css  js  c++  java
  • PAT甲级——A1115 Counting Nodes in a BST【30】

    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 or equal to the node's key.
    • The right subtree of a node contains only nodes with keys greater than the node's key.
    • Both the left and right subtrees must also be binary search trees.

    Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (≤) which is the size of the input sequence. Then given in the next line are the N integers in [which are supposed to be inserted into an initially empty binary search tree.

    Output Specification:

    For each case, print in one line the numbers of nodes in the lowest 2 levels of the resulting tree in the format:

    n1 + n2 = n
    

    where n1 is the number of nodes in the lowest level, n2 is that of the level above, and n is the sum.

    Sample Input:

    9
    25 30 42 16 20 20 35 -5 28
    

    Sample Output:

    2 + 4 = 6

     1 #include <iostream>
     2 #include <queue>
     3 #include <vector>
     4 using namespace std;
     5 struct Node
     6 {
     7     int v;
     8     Node *l, *r;
     9     Node(int a = -1) :v(a), l(nullptr), r(nullptr) {}
    10 };
    11 Node* root = nullptr;
    12 int n, level = -1, a;
    13 vector<int>res;
    14 void creatTree(Node*& root, int x)
    15 {
    16     if (root == nullptr)
    17     {
    18         root = new Node(x);
    19         return;
    20     }
    21     if (x <= root->v)
    22         creatTree(root->l, x);
    23     else
    24         creatTree(root->r, x);
    25 }
    26 void BFS(Node* root)
    27 {
    28     queue<Node*>q;
    29     q.push(root);
    30     while (!q.empty())
    31     {
    32         int num = 0;
    33         queue<Node*>temp;
    34         while (!q.empty())
    35         {
    36             Node* p = q.front();
    37             q.pop();
    38             num++;
    39             if (p->l != nullptr)
    40                 temp.push(p->l);
    41             if (p->r != nullptr)
    42                 temp.push(p->r);
    43         }
    44         q = temp;
    45         res.push_back(num);
    46     }
    47 }
    48 int main()
    49 {
    50 
    51     cin >> n;
    52     while (n--)
    53     {
    54         cin >> a;
    55         creatTree(root, a);
    56     }
    57     BFS(root);
    58     cout << res[res.size() - 1] << " + " << res[res.size() - 2] << " = " << res[res.size() - 1] + res[res.size() - 2] << endl;
    59     return 0;
    60 }
  • 相关阅读:
    Apache虚拟目录的建立
    自制户外登山地图傻瓜书
    经纬度与高克投影转换代码(VB)
    2000国家大地坐标系
    js格式化 Thu Mar 07 2019 12:00:00 GMT+0800 (中国标准时间) 及相互转化
    Javascript农历与公历相互转换
    Numpy
    日期多选插件Kalendae.js
    Scrapy项目实战
    bootstrapdatetimepicker添加支持显示农历节假日信息。
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11457242.html
Copyright © 2011-2022 走看看