第二个测试总WA,初步分析是除根节点外还有节点无“parent”
查看原帖
第二个测试总WA,初步分析是除根节点外还有节点无“parent”
1262290
putian楼主2024/4/5 20:16
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int signal = 0;
	int depth = 0;
	node* left_c = NULL;
	node* right_c = NULL;
	node* parent = NULL;
};
int main() {
	int n;
	cin >> n;
	vector<int>leaf;
	vector<node*>_node(2 * n);
	for (int i = 1; i <= n; i++)
	{
		_node[i] = new node;
	}
	for (int i = 1; i <= n; i++)
	{
		_node[i]->signal = i;
		int l, r;
		cin >> l >> r;
		if (_node[i]->parent != NULL)
		{
			if (l != 0)
			{
				_node[i]->left_c = _node[l];
				_node[l]->parent = _node[i];
			}
			if (r != 0)
			{
				_node[i]->right_c = _node[r];
				_node[r]->parent = _node[i];
			}

			_node[i]->depth = (_node[i]->parent->depth) + 1;
		}
		else if (_node[i]->parent == NULL)
		{
			if (l != 0)
			{
				_node[i]->left_c = _node[l];
				_node[l]->parent = _node[i];
			}
			if (r != 0)
			{
				_node[i]->right_c = _node[r];
				_node[r]->parent = _node[i];
			}
			_node[i]->depth = 1;
		}
		if (l == 0 && r == 0)
		{
			leaf.push_back(_node[i]->depth);
		}
	}
	for (int i = 1; i <= n; i++)
	{
		delete _node[i];
	}

	sort(leaf.begin(), leaf.end());
	cout << leaf.back() << endl;

	system("pause");
	return 0;
}
2024/4/5 20:16
加载中...