寒假每日练习:二叉树

本文分享了解作者在四道编程题目中的学习过程,涉及队列、线段树的数据结构应用,深度优先搜索的算法以及C++中的multiset特性。每道题目都带来了新的知识点和收获。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天又来打卡,写了4道题:P4715,P4913,P1827,P5076

我感觉每道题都可以讲讲,都有收获。

首先是P4715,这个我是用队列写的,但我看了大佬的题解发现了一个新的东西:线段树,就学了学感觉收益颇多,链接在下面:

https://blog.youkuaiyun.com/struct_GS/article/details/120716586

#include<iostream>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;
struct dataa {
	int id;
	int w;
	dataa(int _id, int _w)
	{
		id = _id;
		w = _w;
	}
};
int n;
queue <dataa> q;
int main()
{
	cin >> n;
	for (int i = 1; i <= pow(2, n); i++)
	{
		int tmp;
		cin >> tmp;
		q.push(dataa(i,tmp));
	}
	while (q.size() > 2)
	{
		dataa x1 = q.front();
		q.pop();
		dataa x2 = q.front();
		q.pop();
		if (x1.w > x2.w)
		{
			q.push(x1);
		}
		else {
			q.push(x2);
		}
	}
	dataa x1 = q.front();
	q.pop();
	dataa x2 = q.front();
	q.pop();
	if (x1.w < x2.w)
	{
		cout << x1.id;
	}
	else {
		cout << x2.id;
	}
	return 0;
}

然后是P4913,这个简单直接深搜,这是代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

struct node {
	int id;
	int l, r;
	node() {};
	node(int _id, int _l, int _r)
	{
		id = _id;
		l = _l;
		r = _r;
	}
};
int ans = 0;
int n;
node tree[1000005];
void bfs(int nod, int high)
{
	if (nod == 0)
	{
		ans = max(ans, high);
		return;
	}
	bfs(tree[nod].l, high + 1);
	bfs(tree[nod].r, high + 1);
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		int l, r;
		cin >> l >> r;
		tree[i] = node(i, l, r);
	}
	bfs(1, 0);
	cout << ans;
	return 0;
}

至于P1827,这个就是递归,不过学了一个新函数,string里的subset字符分割用的,有两个变量第一个是切割开始的地点,然后是取多少个字符。这是代码:

#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

char tree[30];
string mid;
string pre;

void work(string mid, string pre)
{
	if (pre.empty())
		return;
	char tmp = pre[0];
	int i = mid.find(tmp);
	string mid1 = mid.substr(0, i);
	string mid2 = mid.substr(i + 1, mid.size() - i - 1);
	string pre1 = pre.substr(1, mid1.size());
	string pre2 = pre.substr(mid1.size() + 1, mid2.size());
	work(mid1, pre1);
	work(mid2, pre2);
	cout << tmp;
}


int main()
{
	cin >> mid >> pre;
	work(mid, pre);
	return 0;
}

最后是P5076,这个我也学到了新东西:multiset,在set中元素不可重复,但是multiset就可以,而且自动排序,刚好可以用来做这题:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <set>

using namespace std;

const int N = 2147483647;
int q, op, x;
multiset <int> m;

int main()
{
	cin >> q;
	while (q--)
	{
		cin >> op;
		if (op == 1)
		{
			cin >> x;
			int se = 0;
			multiset <int>::iterator it;
			it = m.begin();
			for (; it != m.end(); it++)
			{
				if (*it >= x)
				{
					break;
				}
				se++;
			}
			cout << se+1 << endl;
		 }
		else if (op == 2)
		{
			cin >> x;
			multiset <int>::iterator it;
			it = m.begin();
			int se = 0;
			for (; it != m.end(); it++)
			{
				se++;
				if (se == x)
				{
					break;
				}
			}
			cout << *it << endl;
		}
		else if (op == 3)
		{
			cin >> x;
			multiset <int>::iterator it;
			it = m.begin();
			for (; it != m.end(); it++)
			{
				if (*it >= x)
					break;
			}
			if (it == m.begin())
			{
				cout << -N << endl;
			}
			else {
				it--;
				cout << *it << endl;
			}
		}
		else if (op == 4)
		{
			cin >> x;
			multiset <int>::iterator it;
			it = m.begin();
			for (; it != m.end(); it++)
			{
				if (*it > x)
					break;
			}
			if (it == m.end())
			{
				cout << N << endl;
			}
			else
			{
				cout << *it << endl;
			}

		}
		else {
			cin >> x;
			m.insert(x);
		}
	}


	return 0;
}

这就是今天的收获了!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值