#include #include #include #include using namespace std; vector> result; vector leftt, rightt; template struct Node { T element; Node* left; Node* right; Node* parent; Node(const T& e, Node* p) : element(e), parent(p), left(nullptr), right(nullptr) {} }; template class BinTree { public: Node* root; int height; vector arr; BinTree() : root(nullptr), height(0) {} BinTree(const vector& arrs) : arr(arrs), height(0) {} void test() { for (auto x : arr) { cout << x << " "; } } Node* returnroot() { return this->root; } Node* build(int& index, Node* parent) { if (index >= arr.size() || arr[index] == 0) { index++; return nullptr; } Node* node = new Node(arr[index], parent); index++; node->left = build(index, node); node->right = build(index, node); return node; } void buildTree() { int index = 0; this->root = build(index, nullptr); } //输出 void preorder(Node* node) { if (node == nullptr) return; cout << node->element << " "; preorder(node->left); preorder(node->right); } void inorder(Node* node) { if (node == nullptr) return; inorder(node->left); cout << node->element << " "; //cout << ( node->parent == nullptr ? 0 : node->parent->element) << endl; inorder(node->right); } void postorder(Node* node) { if (node == nullptr) return; postorder(node->left); postorder(node->right); cout << node->element << " "; } Node* inorder2(Node* node, const T& x) { if (node == nullptr) return nullptr; if (node->element == x) { return node; } Node* leftResult = inorder2(node->left, x); if (leftResult != nullptr) { return leftResult; } Node* rightResult = inorder2(node->right, x); return rightResult; } void findparent(const T& e) { auto x = inorder2(this->root, e); if (x == nullptr || x->parent == nullptr) cout << 0 << endl; else cout << x->parent->element << endl; } void deletesub(Node* node) { if (node == nullptr) return; deletesub(node->left); deletesub(node->right); delete node; } bool deletenode(const T& e) { auto x = inorder2(this->root, e); if (x == nullptr) return false; if (x == this->root) { deletesub(x); this->root = nullptr; return true; } if (x->parent->left == x) { x->parent->left = nullptr; } else { x->parent->right = nullptr; } deletesub(x); return true; } void searchpath(T a, T b) { Node* na = inorder2(root, a); Node* nb = inorder2(root, b); if (!na || !nb) { cout << 0 << endl << endl; return; } vector*> patha; Node* cur = na; while (cur) { patha.push_back(cur); cur = cur->parent; } reverse(patha.begin(), patha.end()); vector*> pathb; cur = nb; while (cur) { pathb.push_back(cur); cur = cur->parent; } reverse(pathb.begin(), pathb.end()); int lena = patha.size(), lenb = pathb.size(); int minlen = min(lena, lenb); int idx = 0; for (int i = 0; i < minlen; ++i) { if (patha[i] == pathb[i]) idx = i; else break; } vector allpath; for (int j = lena - 1; j > idx; j--) { allpath.push_back(patha[j]->element); } allpath.push_back(patha[idx]->element); for (int j = idx + 1; j < lenb; j++) { allpath.push_back(pathb[j]->element); } int length = allpath.size() - 1; cout << length << endl; for (int i = 0; i < allpath.size(); ++i) { cout << allpath[i] << " "; } cout << endl; } }; int main() { int x = 0; vector arr; string line; getline(cin, line); stringstream ss(line); int num; while (ss >> num) { arr.push_back(num); } BinTree tree(arr); tree.buildTree(); int m; cin >> m; for(int i = 0; i < m; i++){ int a, b; cin >> a >> b; tree.searchpath(a, b); } return 0; }