forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use PascalCase for all structs in C. SImplify n_queens.c Format C code for chapter of graph.
- Loading branch information
Showing
35 changed files
with
502 additions
and
598 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
/** | ||
* File : build_tree.c | ||
* Created Time: 2023-10-16 | ||
* Author : lucas ([email protected]) | ||
*/ | ||
* File : build_tree.c | ||
* Created Time: 2023-10-16 | ||
* Author : lucas ([email protected]) | ||
*/ | ||
|
||
#include "../utils/common.h" | ||
|
||
// 假设所有元素都小于 1000 | ||
#define MAX_N 1000 | ||
|
||
/* 构建二叉树:分治 */ | ||
TreeNode* dfs(int* preorder, int* inorderMap, int i, int l, int r, int size) { | ||
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)); | ||
TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode)); | ||
root->val = preorder[i]; | ||
root->left = NULL; | ||
root->right = NULL; | ||
|
@@ -30,13 +30,13 @@ TreeNode* dfs(int* preorder, int* inorderMap, int i, int l, int r, int size) { | |
} | ||
|
||
/* 构建二叉树 */ | ||
TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) { | ||
TreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) { | ||
// 初始化哈希表,存储 inorder 元素到索引的映射 | ||
int* inorderMap = (int*)malloc(sizeof(int) * MAX_N); | ||
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); | ||
TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize); | ||
free(inorderMap); | ||
return root; | ||
} | ||
|
@@ -52,7 +52,7 @@ int main() { | |
printf("中序遍历 = "); | ||
printArray(inorder, inorderSize); | ||
|
||
TreeNode* root = buildTree(preorder, preorderSize, inorder, inorderSize); | ||
TreeNode *root = buildTree(preorder, preorderSize, inorder, inorderSize); | ||
printf("构建的二叉树为:\n"); | ||
printTree(root); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.