mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +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:
		@ -15,7 +15,7 @@ int *levelOrder(TreeNode *root, int *size) {
 | 
				
			|||||||
    TreeNode **queue;
 | 
					    TreeNode **queue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 辅助队列 */
 | 
					    /* 辅助队列 */
 | 
				
			||||||
    queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
 | 
					    queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_NODE_SIZE);
 | 
				
			||||||
    // 队列指针
 | 
					    // 队列指针
 | 
				
			||||||
    front = 0, rear = 0;
 | 
					    front = 0, rear = 0;
 | 
				
			||||||
    // 加入根节点
 | 
					    // 加入根节点
 | 
				
			||||||
@ -42,6 +42,9 @@ int *levelOrder(TreeNode *root, int *size) {
 | 
				
			|||||||
    // 更新数组长度的值
 | 
					    // 更新数组长度的值
 | 
				
			||||||
    *size = index;
 | 
					    *size = index;
 | 
				
			||||||
    arr = realloc(arr, sizeof(int) * (*size));
 | 
					    arr = realloc(arr, sizeof(int) * (*size));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 释放辅助数组空间
 | 
				
			||||||
 | 
					    free(queue);
 | 
				
			||||||
    return arr;
 | 
					    return arr;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -61,5 +64,8 @@ int main() {
 | 
				
			|||||||
    printf("层序遍历的节点打印序列 = ");
 | 
					    printf("层序遍历的节点打印序列 = ");
 | 
				
			||||||
    printArray(arr, size);
 | 
					    printArray(arr, size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 释放内存
 | 
				
			||||||
 | 
					    freeMemoryTree(root);
 | 
				
			||||||
 | 
					    free(arr);
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -49,7 +49,7 @@ TreeNode *arrToTree(const int *arr, size_t size) {
 | 
				
			|||||||
    /* 根节点 */
 | 
					    /* 根节点 */
 | 
				
			||||||
    root = newTreeNode(arr[0]);
 | 
					    root = newTreeNode(arr[0]);
 | 
				
			||||||
    /* 辅助队列 */
 | 
					    /* 辅助队列 */
 | 
				
			||||||
    queue = (TreeNode **)malloc(sizeof(TreeNode) * MAX_NODE_SIZE);
 | 
					    queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_NODE_SIZE);
 | 
				
			||||||
    // 队列指针
 | 
					    // 队列指针
 | 
				
			||||||
    front = 0, rear = 0;
 | 
					    front = 0, rear = 0;
 | 
				
			||||||
    // 将根节点放入队尾
 | 
					    // 将根节点放入队尾
 | 
				
			||||||
@ -75,6 +75,8 @@ TreeNode *arrToTree(const int *arr, size_t size) {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    // 释放辅助队列空间
 | 
				
			||||||
 | 
					    free(queue);
 | 
				
			||||||
    return root;
 | 
					    return root;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user