mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 12:26:29 +08:00
Fix the problem in binary_tree_bfs.c and the problem that the memory is not released. (#487)
* fix(codes/cpp): Memory leak fix: the space was not freed when pop removed the element. * fix(codes/cpp): Fix access error when printArray(arr, 0) * Update PrintUtil.hpp * fix(codes/c): Fix some errors of cmake build * feat(codes/c): Add hashing_search.c * styles(codes/c): Modify function description * styles(codes/c): Modify binary_search.c code style * fix(codes/c): Fix the problem in binary_tree_bfs.c and the problem that the memory is not released. --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
parent
a6c5f104f1
commit
53ca2144e2
2 changed files with 10 additions and 2 deletions
|
@ -15,7 +15,7 @@ int *levelOrder(TreeNode *root, int *size) {
|
|||
TreeNode **queue;
|
||||
|
||||
/* 辅助队列 */
|
||||
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
||||
queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_NODE_SIZE);
|
||||
// 队列指针
|
||||
front = 0, rear = 0;
|
||||
// 加入根节点
|
||||
|
@ -42,6 +42,9 @@ int *levelOrder(TreeNode *root, int *size) {
|
|||
// 更新数组长度的值
|
||||
*size = index;
|
||||
arr = realloc(arr, sizeof(int) * (*size));
|
||||
|
||||
// 释放辅助数组空间
|
||||
free(queue);
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
@ -61,5 +64,8 @@ int main() {
|
|||
printf("层序遍历的节点打印序列 = ");
|
||||
printArray(arr, size);
|
||||
|
||||
// 释放内存
|
||||
freeMemoryTree(root);
|
||||
free(arr);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ TreeNode *arrToTree(const int *arr, size_t size) {
|
|||
/* 根节点 */
|
||||
root = newTreeNode(arr[0]);
|
||||
/* 辅助队列 */
|
||||
queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
|
||||
queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_NODE_SIZE);
|
||||
// 队列指针
|
||||
front = 0, rear = 0;
|
||||
// 将根节点放入队尾
|
||||
|
@ -75,6 +75,8 @@ TreeNode *arrToTree(const int *arr, size_t size) {
|
|||
}
|
||||
}
|
||||
}
|
||||
// 释放辅助队列空间
|
||||
free(queue);
|
||||
return root;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue