make dfs same as c/c++ and other small improvement (#1543)

This commit is contained in:
rongyi
2024-10-31 21:24:56 +08:00
committed by GitHub
parent 7d708b4fce
commit 68a61f23d5
3 changed files with 38 additions and 29 deletions

View File

@ -126,7 +126,7 @@ impl BinarySearchTree {
// 删除节点 cur
if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {
let left = pre.borrow().left.clone();
if left.is_some() && Rc::ptr_eq(&left.as_ref().unwrap(), &cur) {
if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) {
pre.borrow_mut().left = child;
} else {
pre.borrow_mut().right = child;
@ -147,11 +147,11 @@ impl BinarySearchTree {
break;
}
}
let tmpval = tmp.unwrap().borrow().val;
let tmp_val = tmp.unwrap().borrow().val;
// 递归删除节点 tmp
self.remove(tmpval);
self.remove(tmp_val);
// 用 tmp 覆盖 cur
cur.borrow_mut().val = tmpval;
cur.borrow_mut().val = tmp_val;
}
}
}