求助,3AC,7MLE
查看原帖
求助,3AC,7MLE
1215487
uuuuuuu000楼主2024/10/5 15:59
#include<iostream>
#include<string>

using namespace std;

string str[26];

class TreeNode
{
private:
	char elem;
	TreeNode* lc;
	TreeNode* rc;
public:
	TreeNode(char e, TreeNode* l = NULL, TreeNode* r = NULL) { elem = e; lc = l; rc = r; }
	friend void build(TreeNode*& rt, char e);
	friend void prePrint(TreeNode*& rt);
};

void build(TreeNode*& rt, char e)
{
	if (e == '*')
	{
		rt = NULL;
		return;
	}
	rt = new TreeNode(e);
	int i = 0;
	for (; i < str->size(); i++)
	{
		if (str[i][0] == rt->elem)
		{
			break;
		}
	}
	build(rt->lc, str[i][1]);
	build(rt->rc, str[i][2]);
	return;
}

void prePrint(TreeNode*& rt)
{
	if (rt == NULL)
	{
		return;
	}
	cout << rt->elem;
	prePrint(rt->lc);
	prePrint(rt->rc);
	return;
}

int main()
{
	int n;
	while (cin >> n)
	{
		for (int i = 0; i < n; i++)
		{
			cin >> str[i];
		}
		TreeNode* root;
		build(root, str[0][0]);
		prePrint(root);
	}

	return 0;
}
2024/10/5 15:59
加载中...