关于4个tle,1个re的故事
查看原帖
关于4个tle,1个re的故事
655995
realyanhualengluo楼主2023/2/3 13:58
#include<iostream>
using namespace std;
struct node {
	int data;
	node* lchild;
	node* rchild;
};
node* build(int data) {
	if (data == 0) {
		return NULL;
	}
	node* root = new node();
	root->lchild = root->rchild = NULL;
	root->data = data;
	return root;
}
node* search(node* root, int x) {
	if (root == NULL) {
		return NULL;
	}
	if (root->data == x) {
		return root;
	}
	if (search(root->lchild, x) != NULL) {
		return search(root->lchild, x);
	}
	if (search(root->rchild, x) != NULL) {
		return search(root->rchild, x);
	}
}
int deep(node* root) {
	if (root == NULL){
		return 0;
	}
	int leftdept = deep(root->lchild);
	int rightdept = deep(root->rchild);
	return 1 + max(leftdept, rightdept);
}
int main() {
	int n;
	cin >> n;
	int arr[1000000][2];
	for (int i = 0; i < n; i++) {
		cin >> arr[i][0] >> arr[i][1];
	}
	node* root = build(1);
	root->lchild = build(arr[0][0]);
	root->rchild = build(arr[0][1]);
	for (int i = 1; i < n; i++) {
		node* p = search(root, i + 1);
		p->lchild = build(arr[i][0]);
		p->rchild = build(arr[i][1]);
	}
	cout << deep(root);
}
2023/2/3 13:58
加载中...