This commit is contained in:
krahets
2024-03-21 04:22:07 +08:00
parent 35a07170c0
commit cfdb743939
52 changed files with 292 additions and 290 deletions

View File

@ -638,7 +638,7 @@ comments: true
func levelOrder() -> [Int] {
var res: [Int] = []
// 直接遍历数组
for i in stride(from: 0, to: size(), by: 1) {
for i in 0 ..< size() {
if let val = val(i: i) {
res.append(val)
}

View File

@ -311,7 +311,7 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
/* 获取节点高度 */
func height(node: TreeNode?) -> Int {
// 空节点高度为 -1 ,叶节点高度为 0
node == nil ? -1 : node!.height
node?.height ?? -1
}
/* 更新节点高度 */
@ -2142,7 +2142,7 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
node?.right = removeHelper(node: node?.right, val: val)
} else {
if node?.left == nil || node?.right == nil {
let child = node?.left != nil ? node?.left : node?.right
let child = node?.left ?? node?.right
// 子节点数量 = 0 ,直接删除 node 并返回
if child == nil {
return nil

View File

@ -1106,7 +1106,7 @@ comments: true
// 子节点数量 = 0 or 1
if cur?.left == nil || cur?.right == nil {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
let child = cur?.left != nil ? cur?.left : cur?.right
let child = cur?.left ?? cur?.right
// 删除节点 cur
if cur !== root {
if pre?.left === cur {