PAT甲級1115 Counting Nodes in a BST (30分) 結構體 二叉樹 構造二叉搜索樹,層序遍歷,輸出每層節點個數

1115 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 (≤1000) which is the size of the input sequence. Then given in the next line are the N integers in [−1000,1000] 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

總的來說就是下圖的意思,請叫我靈魂畫手

在這裏插入圖片描述

然後考慮到1000個結點,爲了防止測試點構造出來的數比較偏,我們還是乖乖使用二叉鏈接樹吧,新建一個結點結構體,然後慢慢構造成一個BST樹。
然後關於如何計算最後兩個層次的結點個數,我用的層序遍歷,把每層的元素存到vector< int > vec,然後所有的vec存儲到vector< vector< int>> out裏面
代碼:

#include <iostream>
#include <algorithm>
using namespace std;
#include <vector>
#include <queue>
vector<int> vec;

//1000個節點,雖然感覺測試數據不會是太偏的樹,但還是別偷懶了,直接結構體走起吧
struct node
{
    int data;
    node* lchild,*rchild;
    node():lchild(NULL),rchild(NULL) {}//所有new的node結點的左右孩子都是NULL

};

void df(node *p,int num)
{
    if(num>p->data)
    {
        if(p->rchild!=NULL)//如果右孩子不爲空,就再次深搜
        {
            df(p->rchild,num);
        }
        else//不爲空就新建一個結點,然後鏈接到二叉鏈接樹上
        {
            node *a=new node();
            a->data=num;
            p->rchild=a;
            return ;
        }
    }
    else
    {
        if(p->lchild!=NULL)
        {
            df(p->lchild,num);
        }
        else
        {
            node *a=new node();
            a->data=num;
            p->lchild=a;
            return ;
        }
    }
}
int main()
{
    int N;
    cin>>N;
    node *root;
    for(int i=0; i<N; i++)
    {
        if(i==0)//第一次需要構造一下根節點
        {
            node *a=new node();
            cin>>a->data;
            root=a;
        }
        else
        {
            int num;
            cin>>num;
            df(root,num);
        }
    }
    queue<node*> que;
    que.push(root);//構造隊列用於層序遍歷
    
    vector<vector<int>> out;//out用來記錄每個層次的結點
    
    //下面三行只是確保所給出的二叉樹只有2層,我們需要把根節點也存入到out裏面
    vector<int> vec;
    vec.push_back(root->data);
    out.push_back(vec);
    
    while(!que.empty())
    {
        queue<node*> get;
        vector<int> vec;
        while(!que.empty())
        {
            node *p=que.front();
            int data=p->data;
            if(p->lchild!=NULL)
            {
                get.push(p->lchild);
                vec.push_back(p->lchild->data);
            }

            if(p->rchild!=NULL)
            {
                get.push(p->rchild);
                vec.push_back(p->rchild->data);
            }
            que.pop();
        }
        if(vec.size()!=0)
            out.push_back(vec);
        que=get;
    }

    cout<<out[out.size()-1].size()<<" + "<<out[out.size()-2].size()<<" = "<<out[out.size()-1].size()+out[out.size()-2].size();
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章