• 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 }
  • 相关阅读:
    TControl的主要功能研究(属性,函数,事件)
    写一个控件,如何实现设计期的可视化
    Delphi 通过脚本 在 设计期 改 控件name 属性
    Delphi TreeView – 自动展开树形结构
    Delphi之创建组件模板(Component Template)
    delphi Controls Components 属性
    Delphi 2009 之 TCategoryPanelGroup[1]: ChevronAlignment 等
    delphi xe 10分组按钮CategoryButtons 折叠按钮
    Delphi ControlCount和ComponentCount的区别
    ComponentCount和ControlCount区别
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11457242.html
Copyright © 2020-2023  润新知