PTA二叉搜索树的2层结点统计

PAT (Advanced Level) Practice

标题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

天梯赛模拟赛l2-3
二叉搜索树的2层结点统计 (25分)
二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树:若它的左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉搜索树。

将一系列数字按给定顺序插入一棵初始为空的二叉搜索树,你的任务是统计结果树中最下面 2 层的结点数。

输入格式:输入在第一行给出一个正整数 N (≤1000),为插入数字的个数。第二行给出 N 个 [−1000,1000] 区间内的整数。数字间以空格分隔。

输出格式:在一行中输出最下面 2 层的结点总数。

输入样例:9 25 30 42 16 20 20 35 -5 28 输出样例:6

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+5;
int n;
int l[N], r[N], w[N], idx;
int ans[N], maxn;
void build(int &u, int x){
	if (!u) {
		u = ++idx;
		w[u] = x;
		return;
	}
	if (x <= w[u]) build(l[u], x);
	else build(r[u], x);
}
void dfs(int u, int d){
	if(!u) return;
	ans[d] ++;
	maxn = max(maxn, d);
	dfs(l[u], d+1);
	dfs(r[u], d+1);
}
int main()
{
	cin >> n;
	int root = 0;
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		build(root, x);
	}
	dfs(root, 0);
	printf("%d + %d = %d\n", ans[maxn], ans[maxn-1], ans[maxn-1]+ans[maxn]);
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3+5;
int n;
map<int, vector<int> > mvp;
int l[N], r[N], w[N], idx;
void dfs(int &u, int x, int d){
	if (!u) {
		u = ++idx;
		w[u] = x;
		mvp[d].push_back(x);
		return;
	}
	if (x <= w[u]) dfs(l[u], x, d+1);
	else dfs(r[u], x, d+1);
}
int main()
{
	cin >> n;
	int root = 0;
	if (n <= 1) {
		printf("1 + 0 = 1\n");
		exit(0);
	}
	for (int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		dfs(root, x, 1);
	}
	int ans1 = mvp.rbegin()->second.size();
	mvp.erase(mvp.rbegin()->first);
	int ans2 = mvp.rbegin()->second.size();
	printf("%d + %d = %d\n", ans1, ans2, ans1+ans2);
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值