OJ-二叉树的操作

很容易的一道题,构建树模型就好了

#include <iostream>
#include <vector>
using namespace std;
struct treeNode {
	int val;
	treeNode* left;
	treeNode* right;
	treeNode(int val_) {
		val = val_;
		left = NULL;
		right = NULL;
	}
	treeNode() {
		left = NULL;
		right = NULL;
	}
};
struct info {
	treeNode* node;
	int left, right;
	info(treeNode* node_, int left_, int right_) {
		node = node_;
		left = left_;
		right = right_;
	}
};
treeNode* findParent(treeNode* root, int val) {
	if (!root || (!root->right && !root->left)) {
		return NULL;
	}
	if (((root->left) && (root->left->val == val)) || ((root->right) && (root->right->val == val))) {
		return root;
	}
	treeNode* findLeft = findParent(root->left, val);
	treeNode* findRight = findParent(root->right, val);
	if (!findLeft) {
		return findRight;
	}
	return findLeft;
}
treeNode* findVal(treeNode* root, int val) {
	if (val == -1 || !root) {
		return NULL;
	}
	if (root->val == val) {
		return root;
	}
	if (!root->left && !root->right) {
		return NULL;
	}
	treeNode* findLeft = findVal(root->left, val);
	treeNode* findRight = findVal(root->right, val);
	if (!findLeft) {
		return findRight;
	}
	return findLeft;
}
treeNode* query(treeNode* node) {
	if (!node->left) {
		return node;
	}
	return query(node->left);
}
int main() {
	int t, n, m;
	int X, Y, Z;
	vector<info> infos;
	cin >> t;
	while (t--) {
		infos.clear();
		cin >> n >> m;
		treeNode* root = NULL;
		for (int i = 0; i < n; i++) {
			cin >> X >> Y >> Z;
			treeNode* node = new treeNode();
			node->val = X;
			info mess(node, Y, Z);
			infos.push_back(mess);
		}
		for (int i = 0; i < n; i++) {
			if (infos.at(i).node->val == 0) {
				root = infos.at(i).node;
				break;
			}
		}
		for (int i = 0; i < n; i++) {
			info mess = infos.at(i);
			int left = mess.left;
			int right = mess.right;
			treeNode* leftNode = NULL;
			treeNode* rightNode = NULL;
			for (int j = 0; j < n; j++) {
				if (infos.at(j).node->val == left) {
					leftNode = infos.at(j).node;
				}
				if (infos.at(j).node->val == right) {
					rightNode = infos.at(j).node;
				} 
			}
			mess.node->left = leftNode;
			mess.node->right = rightNode;
		}
		for (int i = 0; i < m; i++) {
			int type;
			cin >> type;
			if (type == 1) {
				int x, y;
				cin >> x >> y;
				treeNode* node1 = findVal(root, x);
				treeNode* node2 = findVal(root, y);
				treeNode* parent1 = findParent(root, x);
				treeNode* parent2 = findParent(root, y);
				
				if (parent1->left && parent1->left->val == x) {
					if (parent2->left && parent2->left->val == y) {
						parent1->left = node2;
						parent2->left = node1;
					} else {
						parent1->left = node2;
						parent2->right = node1;
					}
					
				} else {
					if (parent2->left && parent2->left->val == y) {
						parent1->right = node2;
						parent2->left = node1;
					} else {
						parent1->right = node2;
						parent2->right = node1;
					}
				}
			} else if (type == 2) {
				int x;
				cin >> x;
				treeNode* node = findVal(root, x);
				cout << query(node)->val << endl;
			}
		}
		for (int i = 0; i < n; i++) {
			delete infos.at(i).node;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/pkuout/article/details/80193190