{"id":"3YKqDTLAum","url":"https://pastebin.ca/3YKqDTLAum","raw_url":"https://raw.anybin.ca/3YKqDTLAum","visibility":"public","access":"public","created_at":1796111999461,"expires_at":1796716799461,"fetch_limit":null,"fetches_used":0,"reads_remaining":null,"size_bytes":2815,"syntax_hint":null,"title":null,"filename":null,"change_note":null,"cipher":null,"cipher_meta":null,"parent_id":null,"root_id":"3YKqDTLAum","version":1,"owner_id":null,"recipient_id":null,"body":"#include <iostream>\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\n// 定义二叉树节点结构\nstruct TreeNode {\n    int val;\n    TreeNode *left;\n    TreeNode *right;\n    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n\n// 全局变量存储输入数据\nint n;\nvector<int> in_order;\nvector<int> post_order;\nbool is_complete = true; // 标记是否为完全二叉树\n\n// 根据中序和后序序列重建二叉树\n// in_l, in_r: 当前子树在中序序列中的左右边界（闭区间）\n// post_l, post_r: 当前子树在后序序列中的左右边界（闭区间）\nTreeNode* buildTree(int in_l, int in_r, int post_l, int post_r) {\n    if (in_l > in_r) return nullptr; // 递归终止条件：空区间\n\n    // 后序序列的最后一个元素是根节点\n    int root_val = post_order[post_r];\n    TreeNode* root = new TreeNode(root_val);\n\n    // 在中序序列中找到根节点的位置\n    int root_idx = -1;\n    for (int i = in_l; i <= in_r; ++i) {\n        if (in_order[i] == root_val) {\n            root_idx = i;\n            break;\n        }\n    }\n    // 题目保证输入合理，root_idx必能找到\n\n    // 计算左子树的节点个数\n    int left_size = root_idx - in_l;\n\n    // 递归构建左子树\n    // 中序范围: [in_l, root_idx - 1]\n    // 后序范围: [post_l, post_l + left_size - 1]\n    root->left = buildTree(in_l, root_idx - 1, post_l, post_l + left_size - 1);\n\n    // 递归构建右子树\n    // 中序范围: [root_idx + 1, in_r]\n    // 后序范围: [post_l + left_size, post_r - 1] (注意post_r是根，已排除)\n    root->right = buildTree(root_idx + 1, in_r, post_l + left_size, post_r - 1);\n\n    return root;\n}\n\n// DFS遍历树并检查节点编号\n// u: 当前节点指针\n// id: 当前节点在完全二叉树数组中的下标（从1开始）\nvoid checkComplete(TreeNode* u, long long id) {\n    if (!u) return;\n    if (!is_complete) return; // 若已判定不是，则直接返回\n\n    if (id > n) {\n        is_complete = false; // 发现编号超过节点总数，说明有空洞\n        return;\n    }\n\n    checkComplete(u->left, id * 2);\n    checkComplete(u->right, id * 2 + 1);\n}\n\nint main() {\n    // 优化I/O操作\n    ios::sync_with_stdio(false);\n    cin.tie(nullptr);\n\n    if (!(cin >> n)) return 0; // 读取结点数\n\n    in_order.resize(n);\n    post_order.resize(n);\n\n    for (int i = 0; i < n; ++i) cin >> in_order[i];\n    for (int i = 0; i < n; ++i) cin >> post_order[i];\n\n    // 1. 重建二叉树\n    TreeNode* root = buildTree(0, n - 1, 0, n - 1);\n\n    // 2. 判定完全二叉树\n    checkComplete(root, 1); // 根节点编号为1\n\n    if (is_complete) {\n        cout << \"Yes\" << endl;\n    } else {\n        cout << \"No\" << endl;\n    }\n\n    // 释放内存（可选，竞赛中通常忽略）\n    // ...\n\n    return 0;\n}"}