PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]

题目

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The lef 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 lef 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

题目分析

已知一棵二叉查找树的建树序列,求二叉查找树最后两层的结点数

解题思路

思路 01

  1. 建树(非递归)
  2. bfs深度优先遍历,记录每层结点数和最大层数

思路 02

  1. 建树(递归)
  2. dfs广度优先遍历,记录每层结点数和最大层数

Code

Code 01

#include <iostream>
#include <queue>
using namespace std;
const int maxn=1000;
struct node {
    int data;
    int h=0;
    node * left=NULL;
    node * right=NULL;
    node() {
    }
    node(int _data, int _h) {
        data = _data;
        h=_h;
    }
};
node * root;
int h[maxn],lcn[maxn],max_h;
void insert(int n) {
    if(root==NULL) {
        root=new node(n,1);
        return;
    }
    node * p=root;
    while(p!=NULL) {
        if(p->data<n) {
            if(p->right==NULL){
                p->right=new node(n,p->h+1);
                return;
            }
            else p=p->right;
        } else {
            if(p->left==NULL){
                p->left=new node(n,p->h+1);
                return;
            }
            else p=p->left;
        }
    }
}
void bfs() {
    queue <node *> q;
    q.push(root);
    while(!q.empty()) {
        node * now = q.front();
        q.pop();
        max_h=max(max_h,now->h);
        lcn[now->h]++; 
        if(now->left!=NULL)q.push(now->left);
        if(now->right!=NULL)q.push(now->right);
    }
}
int main(int argc,char * argv[]) {
    int n,rn;
    scanf("%d",&n);
    for(int i=0; i<n; i++) {
        scanf("%d",&rn);
        insert(rn);
    }
    bfs();
    //根节点高度为1,高度0作为哨兵
    printf("%d + %d = %d", lcn[max_h], lcn[max_h-1], lcn[max_h]+lcn[max_h-1]); 
    return 0;
}

Code 02

#include <iostream>
#include <queue>
using namespace std;
const int maxn=1000;
struct node {
    int data;
    int h=0;
    node * left=NULL;
    node * right=NULL;
    node() {
    }
    node(int _data, int _h) {
        data = _data;
        h=_h;
    }
};
node * root;
int h[maxn],lcn[maxn],max_h;
void insert(node* &root, int data, int dep) {
    if(root == NULL) {      //到达空结点时,即为需要插入的位置
        root = new node(data,dep);
        root->data = data;
        root->left = root->right = NULL;    //此句不能漏
        return;
    }
    if(data <= root->data) insert(root->left, data, root->h+1);     //插在左子树
    else insert(root->right, data, root->h+1);     //插在右子树
}
void dfs(node * now, int h){
    if(now==NULL){
        return;
    }
    max_h=max(max_h,h);
    lcn[h]++;
    if(now->left!=NULL)dfs(now->left, h+1);
    if(now->right!=NULL)dfs(now->right, h+1);
}
int main(int argc,char * argv[]) {
    int n,rn;
    scanf("%d",&n);
    for(int i=0; i<n; i++) {
        scanf("%d",&rn);
        insert(root,rn, 1);
    }
    dfs(root,1);
    //根节点高度为1,高度0作为哨兵 
    printf("%d + %d = %d", lcn[max_h], lcn[max_h-1], lcn[max_h]+lcn[max_h-1]); 
    return 0;
}


猜你喜欢

转载自www.cnblogs.com/houzm/p/12329393.html