/** * File : build_tree.c * Created Time: 2023-10-16 * Author : lucas (superrat6@gmail.com) */ #include "../utils/common.h" // 假设所有元素都小于 1000 #define MAX_N 1000 /* 构建二叉树:分治 */ TreeNode* dfs(int* preorder, int* inorderMap, int i, int l, int r, int size) { // 子树区间为空时终止 if (r - l < 0) return NULL; // 初始化根节点 TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode)); root->val = preorder[i]; root->left = NULL; root->right = NULL; // 查询 m ,从而划分左右子树 int m = inorderMap[preorder[i]]; // 子问题:构建左子树 root->left = dfs(preorder, inorderMap, i + 1, l, m - 1, size); // 子问题:构建右子树 root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r, size); // 返回根节点 return root; } /* 构建二叉树 */ TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) { // 初始化哈希表,存储 inorder 元素到索引的映射 int* inorderMap = (int*)malloc(sizeof(int) * MAX_N); for (int i = 0; i < inorderSize; i++) { inorderMap[inorder[i]] = i; } TreeNode* root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize); free(inorderMap); return root; } /* Driver Code */ int main() { int preorder[] = {3, 9, 2, 1, 7}; int inorder[] = {9, 3, 1, 2, 7}; int preorderSize = sizeof(preorder) / sizeof(preorder[0]); int inorderSize = sizeof(inorder) / sizeof(inorder[0]); printf("前序遍历 = "); printArray(preorder, preorderSize); printf("中序遍历 = "); printArray(inorder, inorderSize); TreeNode* root = buildTree(preorder, preorderSize, inorder, inorderSize); printf("构建的二叉树为:\n"); printTree(root); return 0; }