PAT A1115 Counting Nodes in a BST
Sample Input:
9
25 30 42 16 20 20 35 -5 28
Sample Output:
2 + 4 = 6
-
思路 1:
建树,BFS(层序遍历),每个节点设置layer,出队时累加每层dep[layer]的节点数 -
code 1:
#include <string>
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 1010;
int dep[maxn]; //记录每层节点数
struct node{
int data, layer;
node *lc, *rc;
node(){
lc = rc = NULL;
}
};
node* insert(node* &r, int x){
if(r == NULL){
r = new node;
r->data = x;
//TIPS 1: 下面用else if 就不用return(直接进到最后那个return了) , 如果下面单独写成if就需要return回去
// return r;
}else if(x <= r->data){
r->lc = insert(r->lc, x);
}else{
r->rc = insert(r->rc, x);
}
return r;
}
int BFS(node* r){
queue<node*> q;
r->layer = 0;
q.push(r);
while(!q.empty()){
node* now = q.front();
dep[now->layer]++;
q.pop();
if(now->lc != NULL){ //!!!Wrong 1: 这些地方全是now,千万别写成r!!!
now->lc->layer = now->layer+1;
q.push(now->lc);
}
if(now->rc != NULL){
now->rc->layer = now->layer+1;
q.push(now->rc);
}
if(q.empty()) return now->layer;
}
}
int main(){
int n, tmp;
scanf("%d", &n);
node* root = NULL;
for(int i = 0; i < n; ++i){
scanf("%d", &tmp);
root = insert(root, tmp);
}
int depth = BFS(root);
printf("%d + %d = %d", dep[depth], dep[depth-1], dep[depth-1]+dep[depth]);
return 0;
}
-
思路 2:DFS
-
code 2:
int MaxDepth = 0;
void DFS(node* r, int depth){
if(r == NULL){
MaxDepth = max(MaxDepth, depth);
return;
}
dep[depth]++;
DFS(r->lc, depth+1);
DFS(r->rc, depth+1);
}
-
思路:静态数组 + DFS
-
T2 code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
struct Node{
int data, lc, rc;
}node[maxn];
int idex = 0;
int NewNode(int x){
node[idex].data = x;
node[idex].lc = node[idex].rc = -1;
return idex++;
}
void Insert(int & r, int x){
if(r == -1){
r = NewNode(x);
return;
}
if(node[r].data >= x){
Insert(node[r].lc, x);
}else{
Insert(node[r].rc, x);
}
}
int depth[maxn], Depest = 0;
void DFS(int r, int dep){
depth[dep]++;
Depest = max(Depest, dep);
if(node[r].lc != -1) DFS(node[r].lc, dep + 1);
if(node[r].rc != -1) DFS(node[r].rc, dep + 1);
}
int main(){
int n, tmp, root = -1;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &tmp);
Insert(root, tmp);
}
DFS(root, 0);
printf("%d + %d = %d", depth[Depest], depth[Depest-1], depth[Depest] + depth[Depest-1]);
return 0;
}
- T3 code: 在Insert()中直接统计每层节点数,但要【注意】,一定要在最终插入位置累加cnt_dep[], 否则会把插入路径上每层都+1
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
struct node
{
int data;
node *lc, *rc;
};
node* NewNode(int x)
{
node* r = new node;
r->data = x;
r->lc = r->rc = NULL;
return r;
}
int cnt_num[maxn];
void Insert(node* & root, int dep, int x, int & max_dep)
{
if(root == NULL)
{
cnt_num[dep]++; //但要注意,一定要在最终插入位置累加cnt_dep[], 否则会把插入路径上每层都+1
max_dep = max(max_dep, dep);
root = NewNode(x);
return;
}
if(x <= root->data)
{
Insert(root->lc, dep+1, x, max_dep);
}else
{
Insert(root->rc, dep+1, x, max_dep);
}
}
int main()
{
int n, max_dep = 0;
scanf("%d", &n);
node* r = NULL;
for(int i = 0; i < n; ++i)
{
int tmp;
scanf("%d", &tmp);
Insert(r, 0, tmp, max_dep);
}
printf("%d + %d = %d", cnt_num[max_dep], cnt_num[max_dep-1], cnt_num[max_dep-1] + cnt_num[max_dep]);
return 0;
}