mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-16 03:59:18 +08:00
fix binary_search_tree code
This commit is contained in:
@@ -70,8 +70,11 @@ pub fn BinarySearchTree(comptime T: type) type {
|
||||
|
||||
// 插入节点
|
||||
fn insert(self: *Self, num: T) !void {
|
||||
// 若树为空,直接提前返回
|
||||
if (self.root == null) return;
|
||||
// 若树为空,则初始化根节点
|
||||
if (self.root == null) {
|
||||
self.root = try self.mem_allocator.create(inc.TreeNode(T));
|
||||
return;
|
||||
}
|
||||
var cur = self.root;
|
||||
var pre: ?*inc.TreeNode(T) = null;
|
||||
// 循环查找,越过叶节点后跳出
|
||||
|
||||
Reference in New Issue
Block a user