输出父节点和所有子节点
  • 板块灌水区
  • 楼主XTC_lcd
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/6/7 20:49
  • 上次更新2023/10/23 13:42:49
查看原帖
输出父节点和所有子节点
937577
XTC_lcd楼主2023/6/7 20:49

帮忙找找bug吧!我明天就要交作业了QWQ球球了

#include <iostream>
#include <string>
using namespace std;

string k;

struct tree {
	string v;
	tree *lt;
	tree *rt;
};

tree a[101];

void bl(tree *t)
{
	if (t != 0)
	{
		cout << t->v << " ";
		bl(t->lt);
		bl(t->rt);
	}
}

void find_father(tree *t)
{
	if (t != 0)
	{
		if (t->lt->v == k || t->rt->v == k)
		{
			cout << t->v << "\n";
		}
		else
		{
			find_father(t->lt);
			find_father(t->rt);
		}
	}
}

void find_son(tree *t)
{
	if (t != 0)
	{
		if (t->v == k)
		{
			bl(t);
		}
		else
		{
			find_son(t->lt);
			find_son(t->rt);
		}
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].v;
	}
	for (int i = 1; i <= n / 2; i++)
	{
		a[i].lt = &a[2 * i];
		a[i].rt = &a[2 * 1 + 1];
	}
	if (n % 2 == 0)
	{
		a[n / 2].rt = 0;
	}
	cin >> k;
	find_father(&a[1]);
	find_son(&a[1]);
	return 0;
}
2023/6/7 20:49
加载中...