@ -549,14 +549,14 @@
< label class = "md-nav__link" for = "__nav_4" id = "__nav_4_label" tabindex = "0" >
3. 数据结构与类型
3. 数据结构简介
< span class = "md-nav__icon md-icon" > < / span >
< / label >
< nav class = "md-nav" data-md-level = "1" aria-labelledby = "__nav_4_label" aria-expanded = "false" >
< label class = "md-nav__title" for = "__nav_4" >
< span class = "md-nav__icon md-icon" > < / span >
3. 数据结构与类型
3. 数据结构简介
< / label >
< ul class = "md-nav__list" data-md-scrollfix >
@ -2069,7 +2069,7 @@
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > binary_search_tree.py< / span > < pre > < span > < / span > < code > < a id = "__codelineno-2-1" name = "__codelineno-2-1" href = "#__codelineno-2-1" > < / a > < span class = "k" > def< / span > < span class = "nf" > search< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "p" > ,< / span > < span class = "n" > num< / span > < span class = "p" > :< / span > < span class = "nb" > int< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span > < span class = "n" > TreeNode< / span > < span class = "o" > |< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-2" name = "__codelineno-2-2" href = "#__codelineno-2-2" > < / a > < span class = "w" > < / span > < span class = "sd" > " " " 查找节点" " " < / span >
< a id = "__codelineno-2-3" name = "__codelineno-2-3" href = "#__codelineno-2-3" > < / a > < span class = "n" > cur< / span > < span class = "p" > :< / span > < span class = "n" > TreeNode< / span > < span class = "o" > |< / span > < span class = "kc" > None< / span > < span class = "o" > =< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > __ root< / span >
< a id = "__codelineno-2-3" name = "__codelineno-2-3" href = "#__codelineno-2-3" > < / a > < span class = "n" > cur< / span > < span class = "p" > :< / span > < span class = "n" > TreeNode< / span > < span class = "o" > |< / span > < span class = "kc" > None< / span > < span class = "o" > =< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > root< / span >
< a id = "__codelineno-2-4" name = "__codelineno-2-4" href = "#__codelineno-2-4" > < / a > < span class = "c1" > # 循环查找,越过叶节点后跳出< / span >
< a id = "__codelineno-2-5" name = "__codelineno-2-5" href = "#__codelineno-2-5" > < / a > < span class = "k" > while< / span > < span class = "n" > cur< / span > < span class = "ow" > is< / span > < span class = "ow" > not< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-6" name = "__codelineno-2-6" href = "#__codelineno-2-6" > < / a > < span class = "c1" > # 目标节点在 cur 的右子树中< / span >
@ -2172,15 +2172,18 @@
< a id = "__codelineno-7-4" name = "__codelineno-7-4" href = "#__codelineno-7-4" > < / a > < span class = "w" > < / span > < span class = "c1" > // 循环查找,越过叶节点后跳出< / span >
< a id = "__codelineno-7-5" name = "__codelineno-7-5" href = "#__codelineno-7-5" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-7-6" name = "__codelineno-7-6" href = "#__codelineno-7-6" > < / a > < span class = "w" > < / span > < span class = "c1" > // 目标节点在 cur 的右子树中< / span >
< a id = "__codelineno-7-7" name = "__codelineno-7-7" href = "#__codelineno-7-7" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-8" name = "__codelineno-7-8" href = "#__codelineno-7-8" > < / a > < span class = "w" > < / span > < span class = "c1" > // 目标节点在 cur 的左子树中< / span >
< a id = "__codelineno-7-9" name = "__codelineno-7-9" href = "#__codelineno-7-9" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "nf" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-10" name = "__codelineno-7-10" href = "#__codelineno-7-10" > < / a > < span class = "w" > < / span > < span class = "c1" > // 找到目标节点,跳出循环< / span >
< a id = "__codelineno-7-11" name = "__codelineno-7-11" href = "#__codelineno-7-11" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "k" > break< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-12" name = "__codelineno-7-12" href = "#__codelineno-7-12" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-13" name = "__codelineno-7-13" href = "#__codelineno-7-13" > < / a > < span class = "w" > < / span > < span class = "c1" > // 返回目标节点< / span >
< a id = "__codelineno-7-14" name = "__codelineno-7-14" href = "#__codelineno-7-14" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-15" name = "__codelineno-7-15" href = "#__codelineno-7-15" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-7-7" name = "__codelineno-7-7" href = "#__codelineno-7-7" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span >
< a id = "__codelineno-7-8" name = "__codelineno-7-8" href = "#__codelineno-7-8" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-9" name = "__codelineno-7-9" href = "#__codelineno-7-9" > < / a > < span class = "w" > < / span > < span class = "c1" > // 目标节点在 cur 的左子树中< / span >
< a id = "__codelineno-7-10" name = "__codelineno-7-10" href = "#__codelineno-7-10" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "nf" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > > < / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span >
< a id = "__codelineno-7-11" name = "__codelineno-7-11" href = "#__codelineno-7-11" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-12" name = "__codelineno-7-12" href = "#__codelineno-7-12" > < / a > < span class = "w" > < / span > < span class = "c1" > // 找到目标节点,跳出循环< / span >
< a id = "__codelineno-7-13" name = "__codelineno-7-13" href = "#__codelineno-7-13" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span >
< a id = "__codelineno-7-14" name = "__codelineno-7-14" href = "#__codelineno-7-14" > < / a > < span class = "w" > < / span > < span class = "k" > break< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-15" name = "__codelineno-7-15" href = "#__codelineno-7-15" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-7-16" name = "__codelineno-7-16" href = "#__codelineno-7-16" > < / a > < span class = "w" > < / span > < span class = "c1" > // 返回目标节点< / span >
< a id = "__codelineno-7-17" name = "__codelineno-7-17" href = "#__codelineno-7-17" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-7-18" name = "__codelineno-7-18" href = "#__codelineno-7-18" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2305,11 +2308,11 @@
< div class = "highlight" > < span class = "filename" > binary_search_tree.py< / span > < pre > < span > < / span > < code > < a id = "__codelineno-12-1" name = "__codelineno-12-1" href = "#__codelineno-12-1" > < / a > < span class = "k" > def< / span > < span class = "nf" > insert< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "p" > ,< / span > < span class = "n" > num< / span > < span class = "p" > :< / span > < span class = "nb" > int< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-2" name = "__codelineno-12-2" href = "#__codelineno-12-2" > < / a > < span class = "w" > < / span > < span class = "sd" > " " " 插入节点" " " < / span >
< a id = "__codelineno-12-3" name = "__codelineno-12-3" href = "#__codelineno-12-3" > < / a > < span class = "c1" > # 若树为空,直接提前返回< / span >
< a id = "__codelineno-12-4" name = "__codelineno-12-4" href = "#__codelineno-12-4" > < / a > < span class = "k" > if< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > __ root< / span > < span class = "ow" > is< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-4" name = "__codelineno-12-4" href = "#__codelineno-12-4" > < / a > < span class = "k" > if< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > root< / span > < span class = "ow" > is< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-5" name = "__codelineno-12-5" href = "#__codelineno-12-5" > < / a > < span class = "k" > return< / span >
< a id = "__codelineno-12-6" name = "__codelineno-12-6" href = "#__codelineno-12-6" > < / a >
< a id = "__codelineno-12-7" name = "__codelineno-12-7" href = "#__codelineno-12-7" > < / a > < span class = "c1" > # 循环查找,越过叶节点后跳出< / span >
< a id = "__codelineno-12-8" name = "__codelineno-12-8" href = "#__codelineno-12-8" > < / a > < span class = "n" > cur< / span > < span class = "p" > ,< / span > < span class = "n" > pre< / span > < span class = "o" > =< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > __ root< / span > < span class = "p" > ,< / span > < span class = "kc" > None< / span >
< a id = "__codelineno-12-8" name = "__codelineno-12-8" href = "#__codelineno-12-8" > < / a > < span class = "n" > cur< / span > < span class = "p" > ,< / span > < span class = "n" > pre< / span > < span class = "o" > =< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "kc" > None< / span >
< a id = "__codelineno-12-9" name = "__codelineno-12-9" href = "#__codelineno-12-9" > < / a > < span class = "k" > while< / span > < span class = "n" > cur< / span > < span class = "ow" > is< / span > < span class = "ow" > not< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-10" name = "__codelineno-12-10" href = "#__codelineno-12-10" > < / a > < span class = "c1" > # 找到重复节点,直接返回< / span >
< a id = "__codelineno-12-11" name = "__codelineno-12-11" href = "#__codelineno-12-11" > < / a > < span class = "k" > if< / span > < span class = "n" > cur< / span > < span class = "o" > .< / span > < span class = "n" > val< / span > < span class = "o" > ==< / span > < span class = "n" > num< / span > < span class = "p" > :< / span >
@ -2453,26 +2456,32 @@
< div class = "highlight" > < span class = "filename" > binary_search_tree.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-17-1" name = "__codelineno-17-1" href = "#__codelineno-17-1" > < / a > < span class = "cm" > /* 插入节点 */< / span >
< a id = "__codelineno-17-2" name = "__codelineno-17-2" href = "#__codelineno-17-2" > < / a > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > insert< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-3" name = "__codelineno-17-3" href = "#__codelineno-17-3" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若树为空,直接提前返回< / span >
< a id = "__codelineno-17-4" name = "__codelineno-17-4" href = "#__codelineno-17-4" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > root< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-5" name = "__codelineno-17-5" href = "#__codelineno-17-5" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-6" name = "__codelineno-17-6" href = "#__codelineno-17-6" > < / a > < span class = "w" > < / span > < span class = "c1" > // 循环查找,越过叶节点后跳出< / span >
< a id = "__codelineno-17-7" name = "__codelineno-17-7" href = "#__codelineno-17-7" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-8" name = "__codelineno-17-8" href = "#__codelineno-17-8" > < / a > < span class = "w" > < / span > < span class = "c1" > // 找到重复节点,直接返回< / span >
< a id = "__codelineno-17-9" name = "__codelineno-17-9" href = "#__codelineno-17-9" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-10" name = "__codelineno-17-10" href = "#__codelineno-17-10" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-11" name = "__codelineno-17-11" href = "#__codelineno-17-11" > < / a > < span class = "w" > < / span > < span class = "c1" > // 插入位置在 cur 的右子树中< / span >
< a id = "__codelineno-17-12" name = "__codelineno-17-12" href = "#__codelineno-17-12" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-13" name = "__codelineno-17-13" href = "#__codelineno-17-13" > < / a > < span class = "w" > < / span > < span class = "c1" > // 插入位置在 cur 的左子树中< / span >
< a id = "__codelineno-17-14" name = "__codelineno-17-14" href = "#__codelineno-17-14" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-15" name = "__codelineno-17-15" href = "#__codelineno-17-15" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-16" name = "__codelineno-17-16" href = "#__codelineno-17-16" > < / a >
< a id = "__codelineno-17-17" name = "__codelineno-17-17" href = "#__codelineno-17-17" > < / a > < span class = "w" > < / span > < span class = "c1" > // 插入节点< / span >
< a id = "__codelineno-17-18" name = "__codelineno-17-18" href = "#__codelineno-17-18" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "p" > (< / span > < span class = "n" > num< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-19" name = "__codelineno-17-19" href = "#__codelineno-17-19" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-20" name = "__codelineno-17-20" href = "#__codelineno-17-20" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-21" name = "__codelineno-17-21" href = "#__codelineno-17-21" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-22" name = "__codelineno-17-22" href = "#__codelineno-17-22" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-23" name = "__codelineno-17-23" href = "#__codelineno-17-23" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-17-4" name = "__codelineno-17-4" href = "#__codelineno-17-4" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > root< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-5" name = "__codelineno-17-5" href = "#__codelineno-17-5" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-6" name = "__codelineno-17-6" href = "#__codelineno-17-6" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-7" name = "__codelineno-17-7" href = "#__codelineno-17-7" > < / a > < span class = "w" > < / span > < span class = "c1" > // 循环查找,越过叶节点后跳出< / span >
< a id = "__codelineno-17-8" name = "__codelineno-17-8" href = "#__codelineno-17-8" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-9" name = "__codelineno-17-9" href = "#__codelineno-17-9" > < / a > < span class = "w" > < / span > < span class = "c1" > // 找到重复节点,直接返回< / span >
< a id = "__codelineno-17-10" name = "__codelineno-17-10" href = "#__codelineno-17-10" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-11" name = "__codelineno-17-11" href = "#__codelineno-17-11" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-12" name = "__codelineno-17-12" href = "#__codelineno-17-12" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-13" name = "__codelineno-17-13" href = "#__codelineno-17-13" > < / a > < span class = "w" > < / span > < span class = "c1" > // 插入位置在 cur 的右子树中< / span >
< a id = "__codelineno-17-14" name = "__codelineno-17-14" href = "#__codelineno-17-14" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-15" name = "__codelineno-17-15" href = "#__codelineno-17-15" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-16" name = "__codelineno-17-16" href = "#__codelineno-17-16" > < / a > < span class = "w" > < / span > < span class = "c1" > // 插入位置在 cur 的左子树中< / span >
< a id = "__codelineno-17-17" name = "__codelineno-17-17" href = "#__codelineno-17-17" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span >
< a id = "__codelineno-17-18" name = "__codelineno-17-18" href = "#__codelineno-17-18" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-19" name = "__codelineno-17-19" href = "#__codelineno-17-19" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-20" name = "__codelineno-17-20" href = "#__codelineno-17-20" > < / a >
< a id = "__codelineno-17-21" name = "__codelineno-17-21" href = "#__codelineno-17-21" > < / a > < span class = "w" > < / span > < span class = "c1" > // 插入节点< / span >
< a id = "__codelineno-17-22" name = "__codelineno-17-22" href = "#__codelineno-17-22" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > new< / span > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "p" > (< / span > < span class = "n" > num< / span > < span class = "p" > );< / span >
< a id = "__codelineno-17-23" name = "__codelineno-17-23" href = "#__codelineno-17-23" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-17-24" name = "__codelineno-17-24" href = "#__codelineno-17-24" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span >
< a id = "__codelineno-17-25" name = "__codelineno-17-25" href = "#__codelineno-17-25" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-26" name = "__codelineno-17-26" href = "#__codelineno-17-26" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span >
< a id = "__codelineno-17-27" name = "__codelineno-17-27" href = "#__codelineno-17-27" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > node< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-17-28" name = "__codelineno-17-28" href = "#__codelineno-17-28" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-17-29" name = "__codelineno-17-29" href = "#__codelineno-17-29" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2608,24 +2617,29 @@
< a id = "__codelineno-20-25" name = "__codelineno-20-25" href = "#__codelineno-20-25" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当子节点数量 = 0 / 1 时, child = null / 该子节点< / span >
< a id = "__codelineno-20-26" name = "__codelineno-20-26" href = "#__codelineno-20-26" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "na" > left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "kc" > null< / span > < span class = "w" > < / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "na" > left< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "na" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-27" name = "__codelineno-20-27" href = "#__codelineno-20-27" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除节点 cur< / span >
< a id = "__codelineno-20-28" name = "__codelineno-20-28" href = "#__codelineno-20-28" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "na" > left< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > )< / span >
< a id = "__codelineno-20-29" name = "__codelineno-20-29" href = "#__codelineno-20-29" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "na" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-30" name = "__codelineno-20-30" href = "#__codelineno-20-30" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span >
< a id = "__codelineno-20-31" name = "__codelineno-20-31" href = "#__codelineno-20-31" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "na" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-32" name = "__codelineno-20-32" href = "#__codelineno-20-32" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-20-33" name = "__codelineno-20-33" href = "#__codelineno-20-33" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-20-34" name = "__codelineno-20-34" href = "#__codelineno-20-34" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-20-35" name = "__codelineno-20-35" href = "#__codelineno-20-35" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-20-36" name = "__codelineno-20-36" href = "#__codelineno-20-36" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "na" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-37" name = "__codelineno-20-37" href = "#__codelineno-20-37" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "na" > left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "kc" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-20-38" name = "__codelineno-20-38" href = "#__codelineno-20-38" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "na" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-39" name = "__codelineno-20-39" href = "#__codelineno-20-39" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-20-40" name = "__codelineno-20-40" href = "#__codelineno-20-40" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-20-41" name = "__codelineno-20-41" href = "#__codelineno-20-41" > < / a > < span class = "w" > < / span > < span class = "n" > remove< / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "na" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-20-42" name = "__codelineno-20-42" href = "#__codelineno-20-42" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-20-43" name = "__codelineno-20-43" href = "#__codelineno-20-43" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "na" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "na" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-44" name = "__codelineno-20-44" href = "#__codelineno-20-44" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-20-45" name = "__codelineno-20-45" href = "#__codelineno-20-45" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-20-28" name = "__codelineno-20-28" href = "#__codelineno-20-28" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "n" > root< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-20-29" name = "__codelineno-20-29" href = "#__codelineno-20-29" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "na" > left< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > )< / span >
< a id = "__codelineno-20-30" name = "__codelineno-20-30" href = "#__codelineno-20-30" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "na" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-31" name = "__codelineno-20-31" href = "#__codelineno-20-31" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span >
< a id = "__codelineno-20-32" name = "__codelineno-20-32" href = "#__codelineno-20-32" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "na" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-33" name = "__codelineno-20-33" href = "#__codelineno-20-33" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-20-34" name = "__codelineno-20-34" href = "#__codelineno-20-34" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若删除节点为根节点,则重新指定根节点< / span >
< a id = "__codelineno-20-35" name = "__codelineno-20-35" href = "#__codelineno-20-35" > < / a > < span class = "w" > < / span > < span class = "n" > root< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-36" name = "__codelineno-20-36" href = "#__codelineno-20-36" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-20-37" name = "__codelineno-20-37" href = "#__codelineno-20-37" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-20-38" name = "__codelineno-20-38" href = "#__codelineno-20-38" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-20-39" name = "__codelineno-20-39" href = "#__codelineno-20-39" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-20-40" name = "__codelineno-20-40" href = "#__codelineno-20-40" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-20-41" name = "__codelineno-20-41" href = "#__codelineno-20-41" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "na" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-42" name = "__codelineno-20-42" href = "#__codelineno-20-42" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "na" > left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "kc" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-20-43" name = "__codelineno-20-43" href = "#__codelineno-20-43" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "na" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-44" name = "__codelineno-20-44" href = "#__codelineno-20-44" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-20-45" name = "__codelineno-20-45" href = "#__codelineno-20-45" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-20-46" name = "__codelineno-20-46" href = "#__codelineno-20-46" > < / a > < span class = "w" > < / span > < span class = "n" > remove< / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "na" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-20-47" name = "__codelineno-20-47" href = "#__codelineno-20-47" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-20-48" name = "__codelineno-20-48" href = "#__codelineno-20-48" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "na" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "na" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-20-49" name = "__codelineno-20-49" href = "#__codelineno-20-49" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-20-50" name = "__codelineno-20-50" href = "#__codelineno-20-50" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2656,38 +2670,43 @@
< a id = "__codelineno-21-25" name = "__codelineno-21-25" href = "#__codelineno-21-25" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点< / span >
< a id = "__codelineno-21-26" name = "__codelineno-21-26" href = "#__codelineno-21-26" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "n" > child< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "o" > -> < / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > nullptr< / span > < span class = "w" > < / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "o" > -> < / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "o" > -> < / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-27" name = "__codelineno-21-27" href = "#__codelineno-21-27" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除节点 cur< / span >
< a id = "__codelineno-21-28" name = "__codelineno-21-28" href = "#__codelineno-21-28" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > pre< / span > < span class = "o" > -> < / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > )< / span >
< a id = "__codelineno-21-29" name = "__codelineno-21-29" href = "#__codelineno-21-29" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "o" > -> < / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-30" name = "__codelineno-21-30" href = "#__codelineno-21-30" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span >
< a id = "__codelineno-21-31" name = "__codelineno-21-31" href = "#__codelineno-21-31" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "o" > -> < / span > < span class = "n" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-32" name = "__codelineno-21-32" href = "#__codelineno-21-32" > < / a > < span class = "w" > < / span > < span class = "c1" > // 释放内存< / span >
< a id = "__codelineno-21-33" name = "__codelineno-21-33" href = "#__codelineno-21-33" > < / a > < span class = "w" > < / span > < span class = "k" > delete< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-34" name = "__codelineno-21-34" href = "#__codelineno-21-34" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-21-35" name = "__codelineno-21-35" href = "#__codelineno-21-35" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-21-36" name = "__codelineno-21-36" href = "#__codelineno-21-36" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-21-37" name = "__codelineno-21-37" href = "#__codelineno-21-37" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-21-38" name = "__codelineno-21-38" href = "#__codelineno-21-38" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "o" > -> < / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-39" name = "__codelineno-21-39" href = "#__codelineno-21-39" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "o" > -> < / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > nullptr< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-21-40" name = "__codelineno-21-40" href = "#__codelineno-21-40" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "o" > -> < / span > < span class = "n" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-41" name = "__codelineno-21-41" href = "#__codelineno-21-41" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-21-42" name = "__codelineno-21-42" href = "#__codelineno-21-42" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > tmpVal< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "o" > -> < / span > < span class = "n" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-43" name = "__codelineno-21-43" href = "#__codelineno-21-43" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-21-44" name = "__codelineno-21-44" href = "#__codelineno-21-44" > < / a > < span class = "w" > < / span > < span class = "n" > remove< / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "o" > -> < / span > < span class = "n" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-21-45" name = "__codelineno-21-45" href = "#__codelineno-21-45" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-21-46" name = "__codelineno-21-46" href = "#__codelineno-21-46" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "o" > -> < / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmpVal< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-47" name = "__codelineno-21-47" href = "#__codelineno-21-47" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-21-48" name = "__codelineno-21-48" href = "#__codelineno-21-48" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-21-28" name = "__codelineno-21-28" href = "#__codelineno-21-28" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "n" > root< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-21-29" name = "__codelineno-21-29" href = "#__codelineno-21-29" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > pre< / span > < span class = "o" > -> < / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > )< / span >
< a id = "__codelineno-21-30" name = "__codelineno-21-30" href = "#__codelineno-21-30" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "o" > -> < / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-31" name = "__codelineno-21-31" href = "#__codelineno-21-31" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span >
< a id = "__codelineno-21-32" name = "__codelineno-21-32" href = "#__codelineno-21-32" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "o" > -> < / span > < span class = "n" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-33" name = "__codelineno-21-33" href = "#__codelineno-21-33" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-21-34" name = "__codelineno-21-34" href = "#__codelineno-21-34" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若删除节点为根节点,则重新指定根节点< / span >
< a id = "__codelineno-21-35" name = "__codelineno-21-35" href = "#__codelineno-21-35" > < / a > < span class = "w" > < / span > < span class = "n" > root< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-36" name = "__codelineno-21-36" href = "#__codelineno-21-36" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-21-37" name = "__codelineno-21-37" href = "#__codelineno-21-37" > < / a > < span class = "w" > < / span > < span class = "c1" > // 释放内存< / span >
< a id = "__codelineno-21-38" name = "__codelineno-21-38" href = "#__codelineno-21-38" > < / a > < span class = "w" > < / span > < span class = "k" > delete< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-39" name = "__codelineno-21-39" href = "#__codelineno-21-39" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-21-40" name = "__codelineno-21-40" href = "#__codelineno-21-40" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-21-41" name = "__codelineno-21-41" href = "#__codelineno-21-41" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-21-42" name = "__codelineno-21-42" href = "#__codelineno-21-42" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-21-43" name = "__codelineno-21-43" href = "#__codelineno-21-43" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "w" > < / span > < span class = "o" > *< / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "o" > -> < / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-44" name = "__codelineno-21-44" href = "#__codelineno-21-44" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "o" > -> < / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > nullptr< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-21-45" name = "__codelineno-21-45" href = "#__codelineno-21-45" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "o" > -> < / span > < span class = "n" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-46" name = "__codelineno-21-46" href = "#__codelineno-21-46" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-21-47" name = "__codelineno-21-47" href = "#__codelineno-21-47" > < / a > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > tmpVal< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "o" > -> < / span > < span class = "n" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-48" name = "__codelineno-21-48" href = "#__codelineno-21-48" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-21-49" name = "__codelineno-21-49" href = "#__codelineno-21-49" > < / a > < span class = "w" > < / span > < span class = "n" > remove< / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "o" > -> < / span > < span class = "n" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-21-50" name = "__codelineno-21-50" href = "#__codelineno-21-50" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-21-51" name = "__codelineno-21-51" href = "#__codelineno-21-51" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "o" > -> < / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmpVal< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-21-52" name = "__codelineno-21-52" href = "#__codelineno-21-52" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-21-53" name = "__codelineno-21-53" href = "#__codelineno-21-53" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > binary_search_tree.py< / span > < pre > < span > < / span > < code > < a id = "__codelineno-22-1" name = "__codelineno-22-1" href = "#__codelineno-22-1" > < / a > < span class = "k" > def< / span > < span class = "nf" > remove< / span > < span class = "p" > (< / span > < span class = "bp" > self< / span > < span class = "p" > ,< / span > < span class = "n" > num< / span > < span class = "p" > :< / span > < span class = "nb" > int< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-2" name = "__codelineno-22-2" href = "#__codelineno-22-2" > < / a > < span class = "w" > < / span > < span class = "sd" > " " " 删除节点" " " < / span >
< a id = "__codelineno-22-3" name = "__codelineno-22-3" href = "#__codelineno-22-3" > < / a > < span class = "c1" > # 若树为空,直接提前返回< / span >
< a id = "__codelineno-22-4" name = "__codelineno-22-4" href = "#__codelineno-22-4" > < / a > < span class = "k" > if< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > __ root< / span > < span class = "ow" > is< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-4" name = "__codelineno-22-4" href = "#__codelineno-22-4" > < / a > < span class = "k" > if< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > root< / span > < span class = "ow" > is< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-5" name = "__codelineno-22-5" href = "#__codelineno-22-5" > < / a > < span class = "k" > return< / span >
< a id = "__codelineno-22-6" name = "__codelineno-22-6" href = "#__codelineno-22-6" > < / a >
< a id = "__codelineno-22-7" name = "__codelineno-22-7" href = "#__codelineno-22-7" > < / a > < span class = "c1" > # 循环查找,越过叶节点后跳出< / span >
< a id = "__codelineno-22-8" name = "__codelineno-22-8" href = "#__codelineno-22-8" > < / a > < span class = "n" > cur< / span > < span class = "p" > ,< / span > < span class = "n" > pre< / span > < span class = "o" > =< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > __ root< / span > < span class = "p" > ,< / span > < span class = "kc" > None< / span >
< a id = "__codelineno-22-8" name = "__codelineno-22-8" href = "#__codelineno-22-8" > < / a > < span class = "n" > cur< / span > < span class = "p" > ,< / span > < span class = "n" > pre< / span > < span class = "o" > =< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "kc" > None< / span >
< a id = "__codelineno-22-9" name = "__codelineno-22-9" href = "#__codelineno-22-9" > < / a > < span class = "k" > while< / span > < span class = "n" > cur< / span > < span class = "ow" > is< / span > < span class = "ow" > not< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-10" name = "__codelineno-22-10" href = "#__codelineno-22-10" > < / a > < span class = "c1" > # 找到待删除节点,跳出循环< / span >
< a id = "__codelineno-22-11" name = "__codelineno-22-11" href = "#__codelineno-22-11" > < / a > < span class = "k" > if< / span > < span class = "n" > cur< / span > < span class = "o" > .< / span > < span class = "n" > val< / span > < span class = "o" > ==< / span > < span class = "n" > num< / span > < span class = "p" > :< / span >
@ -2708,20 +2727,24 @@
< a id = "__codelineno-22-26" name = "__codelineno-22-26" href = "#__codelineno-22-26" > < / a > < span class = "c1" > # 当子节点数量 = 0 / 1 时, child = null / 该子节点< / span >
< a id = "__codelineno-22-27" name = "__codelineno-22-27" href = "#__codelineno-22-27" > < / a > < span class = "n" > child< / span > < span class = "o" > =< / span > < span class = "n" > cur< / span > < span class = "o" > .< / span > < span class = "n" > left< / span > < span class = "ow" > or< / span > < span class = "n" > cur< / span > < span class = "o" > .< / span > < span class = "n" > right< / span >
< a id = "__codelineno-22-28" name = "__codelineno-22-28" href = "#__codelineno-22-28" > < / a > < span class = "c1" > # 删除节点 cur< / span >
< a id = "__codelineno-22-29" name = "__codelineno-22-29" href = "#__codelineno-22-29" > < / a > < span class = "k" > if< / span > < span class = "n" > pre< / span > < span class = "o" > .< / span > < span class = "n" > left< / span > < span class = "o" > ==< / span > < span class = "n" > cur< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-30" name = "__codelineno-22-30" href = "#__codelineno-22-30" > < / a > < span class = "n" > pre< / span > < span class = "o" > .< / span > < span class = "n" > left< / span > < span class = "o" > =< / span > < span class = "n" > child< / span >
< a id = "__codelineno-22-31" name = "__codelineno-22-31" href = "#__codelineno-22-31" > < / a > < span class = "k" > else< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-32" name = "__codelineno-22-32" href = "#__codelineno-22-32" > < / a > < span class = "n" > pre< / span > < span class = "o" > .< / span > < span class = "n" > right< / span > < span class = "o" > =< / span > < span class = "n" > child< / span >
< a id = "__codelineno-22-33" name = "__codelineno-22-33" href = "#__codelineno-22-33" > < / a > < span class = "c1" > # 子节点数量 = 2< / span >
< a id = "__codelineno-22-34" name = "__codelineno-22-34" href = "#__codelineno-22-34" > < / a > < span class = "k" > else< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-35" name = "__codelineno-22-35" href = "#__codelineno-22-35" > < / a > < span class = "c1" > # 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-22-36" name = "__codelineno-22-36" href = "#__codelineno-22-36" > < / a > < span class = "n" > tmp< / span > < span class = "p" > :< / span > < span class = "n" > TreeNode< / span > < span class = "o" > =< / span > < span class = "n" > cur< / span > < span class = "o" > .< / span > < span class = "n" > right< / span >
< a id = "__codelineno-22-37" name = "__codelineno-22-37" href = "#__codelineno-22-37" > < / a > < span class = "k" > while< / span > < span class = "n" > tmp< / span > < span class = "o" > .< / span > < span class = "n" > left< / span > < span class = "ow" > is< / span > < span class = "ow" > not< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-38" name = "__codelineno-22-38" href = "#__codelineno-22-38" > < / a > < span class = "n" > tmp< / span > < span class = "o" > =< / span > < span class = "n" > tmp< / span > < span class = "o" > .< / span > < span class = "n" > left< / span >
< a id = "__codelineno-22-39" name = "__codelineno-22-39" href = "#__codelineno-22-39" > < / a > < span class = "c1" > # 递归删除节点 tmp< / span >
< a id = "__codelineno-22-40" name = "__codelineno-22-40" href = "#__codelineno-22-40" > < / a > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > remove< / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "o" > .< / span > < span class = "n" > val< / span > < span class = "p" > )< / span >
< a id = "__codelineno-22-41" name = "__codelineno-22-41" href = "#__codelineno-22-41" > < / a > < span class = "c1" > # 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-22-42" name = "__codelineno-22-42" href = "#__codelineno-22-42" > < / a > < span class = "n" > cur< / span > < span class = "o" > .< / span > < span class = "n" > val< / span > < span class = "o" > =< / span > < span class = "n" > tmp< / span > < span class = "o" > .< / span > < span class = "n" > val< / span >
< a id = "__codelineno-22-29" name = "__codelineno-22-29" href = "#__codelineno-22-29" > < / a > < span class = "k" > if< / span > < span class = "n" > cur< / span > < span class = "o" > !=< / span > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > root< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-30" name = "__codelineno-22-30" href = "#__codelineno-22-30" > < / a > < span class = "k" > if< / span > < span class = "n" > pre< / span > < span class = "o" > .< / span > < span class = "n" > left< / span > < span class = "o" > ==< / span > < span class = "n" > cur< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-31" name = "__codelineno-22-31" href = "#__codelineno-22-31" > < / a > < span class = "n" > pre< / span > < span class = "o" > .< / span > < span class = "n" > left< / span > < span class = "o" > =< / span > < span class = "n" > child< / span >
< a id = "__codelineno-22-32" name = "__codelineno-22-32" href = "#__codelineno-22-32" > < / a > < span class = "k" > else< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-33" name = "__codelineno-22-33" href = "#__codelineno-22-33" > < / a > < span class = "n" > pre< / span > < span class = "o" > .< / span > < span class = "n" > right< / span > < span class = "o" > =< / span > < span class = "n" > child< / span >
< a id = "__codelineno-22-34" name = "__codelineno-22-34" href = "#__codelineno-22-34" > < / a > < span class = "k" > else< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-35" name = "__codelineno-22-35" href = "#__codelineno-22-35" > < / a > < span class = "c1" > # 若删除节点为根节点,则重新指定根节点< / span >
< a id = "__codelineno-22-36" name = "__codelineno-22-36" href = "#__codelineno-22-36" > < / a > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > __root< / span > < span class = "o" > =< / span > < span class = "n" > cur< / span >
< a id = "__codelineno-22-37" name = "__codelineno-22-37" href = "#__codelineno-22-37" > < / a > < span class = "c1" > # 子节点数量 = 2< / span >
< a id = "__codelineno-22-38" name = "__codelineno-22-38" href = "#__codelineno-22-38" > < / a > < span class = "k" > else< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-39" name = "__codelineno-22-39" href = "#__codelineno-22-39" > < / a > < span class = "c1" > # 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-22-40" name = "__codelineno-22-40" href = "#__codelineno-22-40" > < / a > < span class = "n" > tmp< / span > < span class = "p" > :< / span > < span class = "n" > TreeNode< / span > < span class = "o" > =< / span > < span class = "n" > cur< / span > < span class = "o" > .< / span > < span class = "n" > right< / span >
< a id = "__codelineno-22-41" name = "__codelineno-22-41" href = "#__codelineno-22-41" > < / a > < span class = "k" > while< / span > < span class = "n" > tmp< / span > < span class = "o" > .< / span > < span class = "n" > left< / span > < span class = "ow" > is< / span > < span class = "ow" > not< / span > < span class = "kc" > None< / span > < span class = "p" > :< / span >
< a id = "__codelineno-22-42" name = "__codelineno-22-42" href = "#__codelineno-22-42" > < / a > < span class = "n" > tmp< / span > < span class = "o" > =< / span > < span class = "n" > tmp< / span > < span class = "o" > .< / span > < span class = "n" > left< / span >
< a id = "__codelineno-22-43" name = "__codelineno-22-43" href = "#__codelineno-22-43" > < / a > < span class = "c1" > # 递归删除节点 tmp< / span >
< a id = "__codelineno-22-44" name = "__codelineno-22-44" href = "#__codelineno-22-44" > < / a > < span class = "bp" > self< / span > < span class = "o" > .< / span > < span class = "n" > remove< / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "o" > .< / span > < span class = "n" > val< / span > < span class = "p" > )< / span >
< a id = "__codelineno-22-45" name = "__codelineno-22-45" href = "#__codelineno-22-45" > < / a > < span class = "c1" > # 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-22-46" name = "__codelineno-22-46" href = "#__codelineno-22-46" > < / a > < span class = "n" > cur< / span > < span class = "o" > .< / span > < span class = "n" > val< / span > < span class = "o" > =< / span > < span class = "n" > tmp< / span > < span class = "o" > .< / span > < span class = "n" > val< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2761,25 +2784,30 @@
< a id = "__codelineno-23-34" name = "__codelineno-23-34" href = "#__codelineno-23-34" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-35" name = "__codelineno-23-35" href = "#__codelineno-23-35" > < / a > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > Right< / span >
< a id = "__codelineno-23-36" name = "__codelineno-23-36" href = "#__codelineno-23-36" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-23-37" name = "__codelineno-23-37" href = "#__codelineno-23-37" > < / a > < span class = "w" > < / span > < span class = "c1" > // 将子节点替换为待删除节点< / span >
< a id = "__codelineno-23-38" name = "__codelineno-23-38" href = "#__codelineno-23-38" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > Left< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-39" name = "__codelineno-23-39" href = "#__codelineno-23-39" > < / a > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > Left< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span >
< a id = "__codelineno-23-40" name = "__codelineno-23-40" href = "#__codelineno-23-40" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-41" name = "__codelineno-23-41" href = "#__codelineno-23-41" > < / a > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > Right< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span >
< a id = "__codelineno-23-42" name = "__codelineno-23-42" href = "#__codelineno-23-42" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-23-43" name = "__codelineno-23-43" href = "#__codelineno-23-43" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数为 2< / span >
< a id = "__codelineno-23-44" name = "__codelineno-23-44" href = "#__codelineno-23-44" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-45" name = "__codelineno-23-45" href = "#__codelineno-23-45" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中待删除节点 cur 的下一个节点< / span >
< a id = "__codelineno-23-46" name = "__codelineno-23-46" href = "#__codelineno-23-46" > < / a > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > Right< / span >
< a id = "__codelineno-23-47" name = "__codelineno-23-47" href = "#__codelineno-23-47" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > Left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "kc" > nil< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-48" name = "__codelineno-23-48" href = "#__codelineno-23-48" > < / a > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > Left< / span >
< a id = "__codelineno-23-49" name = "__codelineno-23-49" href = "#__codelineno-23-49" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-23-50" name = "__codelineno-23-50" href = "#__codelineno-23-50" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-23-51" name = "__codelineno-23-51" href = "#__codelineno-23-51" > < / a > < span class = "w" > < / span > < span class = "nx" > bst< / span > < span class = "p" > .< / span > < span class = "nx" > remove< / span > < span class = "p" > (< / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > Val< / span > < span class = "p" > )< / span >
< a id = "__codelineno-23-52" name = "__codelineno-23-52" href = "#__codelineno-23-52" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-23-53" name = "__codelineno-23-53" href = "#__codelineno-23-53" > < / a > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > Val< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > Val< / span >
< a id = "__codelineno-23-54" name = "__codelineno-23-54" href = "#__codelineno-23-54" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-23-55" name = "__codelineno-23-55" href = "#__codelineno-23-55" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-23-37" name = "__codelineno-23-37" href = "#__codelineno-23-37" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除节点 cur< / span >
< a id = "__codelineno-23-38" name = "__codelineno-23-38" href = "#__codelineno-23-38" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "nx" > bst< / span > < span class = "p" > .< / span > < span class = "nx" > root< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-39" name = "__codelineno-23-39" href = "#__codelineno-23-39" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > Left< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-40" name = "__codelineno-23-40" href = "#__codelineno-23-40" > < / a > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > Left< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span >
< a id = "__codelineno-23-41" name = "__codelineno-23-41" href = "#__codelineno-23-41" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-42" name = "__codelineno-23-42" href = "#__codelineno-23-42" > < / a > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > Right< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span >
< a id = "__codelineno-23-43" name = "__codelineno-23-43" href = "#__codelineno-23-43" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-23-44" name = "__codelineno-23-44" href = "#__codelineno-23-44" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-45" name = "__codelineno-23-45" href = "#__codelineno-23-45" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若删除节点为根节点,则重新指定根节点< / span >
< a id = "__codelineno-23-46" name = "__codelineno-23-46" href = "#__codelineno-23-46" > < / a > < span class = "w" > < / span > < span class = "nx" > bst< / span > < span class = "p" > .< / span > < span class = "nx" > root< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span >
< a id = "__codelineno-23-47" name = "__codelineno-23-47" href = "#__codelineno-23-47" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-23-48" name = "__codelineno-23-48" href = "#__codelineno-23-48" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数为 2< / span >
< a id = "__codelineno-23-49" name = "__codelineno-23-49" href = "#__codelineno-23-49" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-50" name = "__codelineno-23-50" href = "#__codelineno-23-50" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中待删除节点 cur 的下一个节点< / span >
< a id = "__codelineno-23-51" name = "__codelineno-23-51" href = "#__codelineno-23-51" > < / a > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > Right< / span >
< a id = "__codelineno-23-52" name = "__codelineno-23-52" href = "#__codelineno-23-52" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > Left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "kc" > nil< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-23-53" name = "__codelineno-23-53" href = "#__codelineno-23-53" > < / a > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > Left< / span >
< a id = "__codelineno-23-54" name = "__codelineno-23-54" href = "#__codelineno-23-54" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-23-55" name = "__codelineno-23-55" href = "#__codelineno-23-55" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-23-56" name = "__codelineno-23-56" href = "#__codelineno-23-56" > < / a > < span class = "w" > < / span > < span class = "nx" > bst< / span > < span class = "p" > .< / span > < span class = "nx" > remove< / span > < span class = "p" > (< / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > Val< / span > < span class = "p" > )< / span >
< a id = "__codelineno-23-57" name = "__codelineno-23-57" href = "#__codelineno-23-57" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-23-58" name = "__codelineno-23-58" href = "#__codelineno-23-58" > < / a > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > Val< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > Val< / span >
< a id = "__codelineno-23-59" name = "__codelineno-23-59" href = "#__codelineno-23-59" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-23-60" name = "__codelineno-23-60" href = "#__codelineno-23-60" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2806,22 +2834,27 @@
< a id = "__codelineno-24-21" name = "__codelineno-24-21" href = "#__codelineno-24-21" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当子节点数量 = 0 / 1 时, child = null / 该子节点< / span >
< a id = "__codelineno-24-22" name = "__codelineno-24-22" href = "#__codelineno-24-22" > < / a > < span class = "w" > < / span > < span class = "kd" > let< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > !==< / span > < span class = "w" > < / span > < span class = "kc" > null< / span > < span class = "w" > < / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-23" name = "__codelineno-24-23" href = "#__codelineno-24-23" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除节点 cur< / span >
< a id = "__codelineno-24-24" name = "__codelineno-24-24" href = "#__codelineno-24-24" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > ===< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-25" name = "__codelineno-24-25" href = "#__codelineno-24-25" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-26" name = "__codelineno-24-26" href = "#__codelineno-24-26" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-24-27" name = "__codelineno-24-27" href = "#__codelineno-24-27" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-24-28" name = "__codelineno-24-28" href = "#__codelineno-24-28" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-24-29" name = "__codelineno-24-29" href = "#__codelineno-24-29" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-24-30" name = "__codelineno-24-30" href = "#__codelineno-24-30" > < / a > < span class = "w" > < / span > < span class = "kd" > let< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-31" name = "__codelineno-24-31" href = "#__codelineno-24-31" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > !==< / span > < span class = "w" > < / span > < span class = "kc" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-24-32" name = "__codelineno-24-32" href = "#__codelineno-24-32" > < / a > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-33" name = "__codelineno-24-33" href = "#__codelineno-24-33" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-24-34" name = "__codelineno-24-34" href = "#__codelineno-24-34" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-24-35" name = "__codelineno-24-35" href = "#__codelineno-24-35" > < / a > < span class = "w" > < / span > < span class = "nx" > remove< / span > < span class = "p" > (< / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-24-36" name = "__codelineno-24-36" href = "#__codelineno-24-36" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-24-37" name = "__codelineno-24-37" href = "#__codelineno-24-37" > < / a > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-38" name = "__codelineno-24-38" href = "#__codelineno-24-38" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-24-39" name = "__codelineno-24-39" href = "#__codelineno-24-39" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-24-24" name = "__codelineno-24-24" href = "#__codelineno-24-24" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "nx" > root< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-24-25" name = "__codelineno-24-25" href = "#__codelineno-24-25" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > ===< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-26" name = "__codelineno-24-26" href = "#__codelineno-24-26" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "p" > .< / span > < span class = "nx" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-27" name = "__codelineno-24-27" href = "#__codelineno-24-27" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-24-28" name = "__codelineno-24-28" href = "#__codelineno-24-28" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若删除节点为根节点,则重新指定根节点< / span >
< a id = "__codelineno-24-29" name = "__codelineno-24-29" href = "#__codelineno-24-29" > < / a > < span class = "w" > < / span > < span class = "nx" > root< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-30" name = "__codelineno-24-30" href = "#__codelineno-24-30" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-24-31" name = "__codelineno-24-31" href = "#__codelineno-24-31" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-24-32" name = "__codelineno-24-32" href = "#__codelineno-24-32" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-24-33" name = "__codelineno-24-33" href = "#__codelineno-24-33" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-24-34" name = "__codelineno-24-34" href = "#__codelineno-24-34" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-24-35" name = "__codelineno-24-35" href = "#__codelineno-24-35" > < / a > < span class = "w" > < / span > < span class = "kd" > let< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-36" name = "__codelineno-24-36" href = "#__codelineno-24-36" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > !==< / span > < span class = "w" > < / span > < span class = "kc" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-24-37" name = "__codelineno-24-37" href = "#__codelineno-24-37" > < / a > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-38" name = "__codelineno-24-38" href = "#__codelineno-24-38" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-24-39" name = "__codelineno-24-39" href = "#__codelineno-24-39" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-24-40" name = "__codelineno-24-40" href = "#__codelineno-24-40" > < / a > < span class = "w" > < / span > < span class = "nx" > remove< / span > < span class = "p" > (< / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-24-41" name = "__codelineno-24-41" href = "#__codelineno-24-41" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-24-42" name = "__codelineno-24-42" href = "#__codelineno-24-42" > < / a > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-24-43" name = "__codelineno-24-43" href = "#__codelineno-24-43" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-24-44" name = "__codelineno-24-44" href = "#__codelineno-24-44" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2855,25 +2888,30 @@
< a id = "__codelineno-25-28" name = "__codelineno-25-28" href = "#__codelineno-25-28" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当子节点数量 = 0 / 1 时, child = null / 该子节点< / span >
< a id = "__codelineno-25-29" name = "__codelineno-25-29" href = "#__codelineno-25-29" > < / a > < span class = "w" > < / span > < span class = "kd" > let< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > !==< / span > < span class = "w" > < / span > < span class = "kc" > null< / span > < span class = "w" > < / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "nx" > cur.left< / span > < span class = "w" > < / span > < span class = "o" > :< / span > < span class = "w" > < / span > < span class = "kt" > cur.right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-30" name = "__codelineno-25-30" href = "#__codelineno-25-30" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除节点 cur< / span >
< a id = "__codelineno-25-31" name = "__codelineno-25-31" href = "#__codelineno-25-31" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > pre< / span > < span class = "o" > !< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > ===< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-25-32" name = "__codelineno-25-32" href = "#__codelineno-25-32" > < / a > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "o" > !< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-33" name = "__codelineno-25-33" href = "#__codelineno-25-33" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-25-34" name = "__codelineno-25-34" href = "#__codelineno-25-34" > < / a > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "o" > !< / span > < span class = "p" > .< / span > < span class = "nx" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-35" name = "__codelineno-25-35" href = "#__codelineno-25-35" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-25-36" name = "__codelineno-25-36" href = "#__codelineno-25-36" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-25-37" name = "__codelineno-25-37" href = "#__codelineno-25-37" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-25-38" name = "__codelineno-25-38" href = "#__codelineno-25-38" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-25-39" name = "__codelineno-25-39" href = "#__codelineno-25-39" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-25-40" name = "__codelineno-25-40" href = "#__codelineno-25-40" > < / a > < span class = "w" > < / span > < span class = "kd" > let< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-41" name = "__codelineno-25-41" href = "#__codelineno-25-41" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > !==< / span > < span class = "w" > < / span > < span class = "kc" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-25-42" name = "__codelineno-25-42" href = "#__codelineno-25-42" > < / a > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-43" name = "__codelineno-25-43" href = "#__codelineno-25-43" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-25-44" name = "__codelineno-25-44" href = "#__codelineno-25-44" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-25-45" name = "__codelineno-25-45" href = "#__codelineno-25-45" > < / a > < span class = "w" > < / span > < span class = "nx" > remove< / span > < span class = "p" > (< / span > < span class = "nx" > tmp< / span > < span class = "o" > !< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-25-46" name = "__codelineno-25-46" href = "#__codelineno-25-46" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-25-47" name = "__codelineno-25-47" href = "#__codelineno-25-47" > < / a > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-48" name = "__codelineno-25-48" href = "#__codelineno-25-48" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-25-49" name = "__codelineno-25-49" href = "#__codelineno-25-49" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-25-31" name = "__codelineno-25-31" href = "#__codelineno-25-31" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "nx" > root< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-25-32" name = "__codelineno-25-32" href = "#__codelineno-25-32" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > pre< / span > < span class = "o" > !< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > ===< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-25-33" name = "__codelineno-25-33" href = "#__codelineno-25-33" > < / a > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "o" > !< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-34" name = "__codelineno-25-34" href = "#__codelineno-25-34" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-25-35" name = "__codelineno-25-35" href = "#__codelineno-25-35" > < / a > < span class = "w" > < / span > < span class = "nx" > pre< / span > < span class = "o" > !< / span > < span class = "p" > .< / span > < span class = "nx" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-36" name = "__codelineno-25-36" href = "#__codelineno-25-36" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-25-37" name = "__codelineno-25-37" href = "#__codelineno-25-37" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-25-38" name = "__codelineno-25-38" href = "#__codelineno-25-38" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若删除节点为根节点,则重新指定根节点< / span >
< a id = "__codelineno-25-39" name = "__codelineno-25-39" href = "#__codelineno-25-39" > < / a > < span class = "w" > < / span > < span class = "nx" > root< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-40" name = "__codelineno-25-40" href = "#__codelineno-25-40" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-25-41" name = "__codelineno-25-41" href = "#__codelineno-25-41" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-25-42" name = "__codelineno-25-42" href = "#__codelineno-25-42" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-25-43" name = "__codelineno-25-43" href = "#__codelineno-25-43" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-25-44" name = "__codelineno-25-44" href = "#__codelineno-25-44" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-25-45" name = "__codelineno-25-45" href = "#__codelineno-25-45" > < / a > < span class = "w" > < / span > < span class = "kd" > let< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-46" name = "__codelineno-25-46" href = "#__codelineno-25-46" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "w" > < / span > < span class = "o" > !==< / span > < span class = "w" > < / span > < span class = "kc" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-25-47" name = "__codelineno-25-47" href = "#__codelineno-25-47" > < / a > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-48" name = "__codelineno-25-48" href = "#__codelineno-25-48" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-25-49" name = "__codelineno-25-49" href = "#__codelineno-25-49" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-25-50" name = "__codelineno-25-50" href = "#__codelineno-25-50" > < / a > < span class = "w" > < / span > < span class = "nx" > remove< / span > < span class = "p" > (< / span > < span class = "nx" > tmp< / span > < span class = "o" > !< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-25-51" name = "__codelineno-25-51" href = "#__codelineno-25-51" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-25-52" name = "__codelineno-25-52" href = "#__codelineno-25-52" > < / a > < span class = "w" > < / span > < span class = "nx" > cur< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "nx" > tmp< / span > < span class = "p" > .< / span > < span class = "nx" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-25-53" name = "__codelineno-25-53" href = "#__codelineno-25-53" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-25-54" name = "__codelineno-25-54" href = "#__codelineno-25-54" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2932,44 +2970,53 @@
< div class = "highlight" > < span class = "filename" > binary_search_tree.cs< / span > < pre > < span > < / span > < code > < a id = "__codelineno-27-1" name = "__codelineno-27-1" href = "#__codelineno-27-1" > < / a > < span class = "cm" > /* 删除节点 */< / span >
< a id = "__codelineno-27-2" name = "__codelineno-27-2" href = "#__codelineno-27-2" > < / a > < span class = "k" > void< / span > < span class = "w" > < / span > < span class = "nf" > remove< / span > < span class = "p" > (< / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-3" name = "__codelineno-27-3" href = "#__codelineno-27-3" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若树为空,直接提前返回< / span >
< a id = "__codelineno-27-4" name = "__codelineno-27-4" href = "#__codelineno-27-4" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > root< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-5" name = "__codelineno-27-5" href = "#__codelineno-27-5" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-6" name = "__codelineno-27-6" href = "#__codelineno-27-6" > < / a > < span class = "w" > < / span > < span class = "c1" > // 循环查找,越过叶节点后跳出< / span >
< a id = "__codelineno-27-7" name = "__codelineno-27-7" href = "#__codelineno-27-7" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-8" name = "__codelineno-27-8" href = "#__codelineno-27-8" > < / a > < span class = "w" > < / span > < span class = "c1" > // 找到待删除节点,跳出循环< / span >
< a id = "__codelineno-27-9" name = "__codelineno-27-9" href = "#__codelineno-27-9" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "k" > break< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-10" name = "__codelineno-27-10" href = "#__codelineno-27-10" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-11" name = "__codelineno-27-11" href = "#__codelineno-27-11" > < / a > < span class = "w" > < / span > < span class = "c1" > // 待删除节点在 cur 的右子树中< / span >
< a id = "__codelineno-27-12" name = "__codelineno-27-12" href = "#__codelineno-27-12" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-13" name = "__codelineno-27-13" href = "#__codelineno-27-13" > < / a > < span class = "w" > < / span > < span class = "c1" > // 待删除节点在 cur 的左子树中< / span >
< a id = "__codelineno-27-14" name = "__codelineno-27-14" href = "#__codelineno-27-14" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-15" name = "__codelineno-27-15" href = "#__codelineno-27-15" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-16" name = "__codelineno-27-16" href = "#__codelineno-27-16" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若无待删除节点,则直接返回< / span >
< a id = "__codelineno-27-17" name = "__codelineno-27-17" href = "#__codelineno-27-17" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-18" name = "__codelineno-27-18" href = "#__codelineno-27-18" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 0 or 1< / span >
< a id = "__codelineno-27-19" name = "__codelineno-27-19" href = "#__codelineno-27-19" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-20" name = "__codelineno-27-20" href = "#__codelineno-27-20" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当子节点数量 = 0 / 1 时, child = null / 该子节点< / span >
< a id = "__codelineno-27-21" name = "__codelineno-27-21" href = "#__codelineno-27-21" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "w" > < / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-22" name = "__codelineno-27-22" href = "#__codelineno-27-22" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除节点 cur< / span >
< a id = "__codelineno-27-23" name = "__codelineno-27-23" href = "#__codelineno-27-23" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-24" name = "__codelineno-27-24" href = "#__codelineno-27-24" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-25" name = "__codelineno-27-25" href = "#__codelineno-27-25" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-26" name = "__codelineno-27-26" href = "#__codelineno-27-26" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-27" name = "__codelineno-27-27" href = "#__codelineno-27-27" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-28" name = "__codelineno-27-28" href = "#__codelineno-27-28" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-29" name = "__codelineno-27-29" href = "#__codelineno-27-29" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-27-30" name = "__codelineno-27-30" href = "#__codelineno-27-30" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-31" name = "__codelineno-27-31" href = "#__codelineno-27-31" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-27-32" name = "__codelineno-27-32" href = "#__codelineno-27-32" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-33" name = "__codelineno-27-33" href = "#__codelineno-27-33" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-34" name = "__codelineno-27-34" href = "#__codelineno-27-34" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-35" name = "__codelineno-27-35" href = "#__codelineno-27-35" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-36" name = "__codelineno-27-36" href = "#__codelineno-27-36" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-27-37" name = "__codelineno-27-37" href = "#__codelineno-27-37" > < / a > < span class = "w" > < / span > < span class = "k" > remove< / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-27-38" name = "__codelineno-27-38" href = "#__codelineno-27-38" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-27-39" name = "__codelineno-27-39" href = "#__codelineno-27-39" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-40" name = "__codelineno-27-40" href = "#__codelineno-27-40" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-41" name = "__codelineno-27-41" href = "#__codelineno-27-41" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-27-4" name = "__codelineno-27-4" href = "#__codelineno-27-4" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > root< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span >
< a id = "__codelineno-27-5" name = "__codelineno-27-5" href = "#__codelineno-27-5" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-6" name = "__codelineno-27-6" href = "#__codelineno-27-6" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > root< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-7" name = "__codelineno-27-7" href = "#__codelineno-27-7" > < / a > < span class = "w" > < / span > < span class = "c1" > // 循环查找,越过叶节点后跳出< / span >
< a id = "__codelineno-27-8" name = "__codelineno-27-8" href = "#__codelineno-27-8" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-9" name = "__codelineno-27-9" href = "#__codelineno-27-9" > < / a > < span class = "w" > < / span > < span class = "c1" > // 找到待删除节点,跳出循环< / span >
< a id = "__codelineno-27-10" name = "__codelineno-27-10" href = "#__codelineno-27-10" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span >
< a id = "__codelineno-27-11" name = "__codelineno-27-11" href = "#__codelineno-27-11" > < / a > < span class = "w" > < / span > < span class = "k" > break< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-12" name = "__codelineno-27-12" href = "#__codelineno-27-12" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-13" name = "__codelineno-27-13" href = "#__codelineno-27-13" > < / a > < span class = "w" > < / span > < span class = "c1" > // 待删除节点在 cur 的右子树中< / span >
< a id = "__codelineno-27-14" name = "__codelineno-27-14" href = "#__codelineno-27-14" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > < < / span > < span class = "w" > < / span > < span class = "n" > num< / span > < span class = "p" > )< / span >
< a id = "__codelineno-27-15" name = "__codelineno-27-15" href = "#__codelineno-27-15" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-16" name = "__codelineno-27-16" href = "#__codelineno-27-16" > < / a > < span class = "w" > < / span > < span class = "c1" > // 待删除节点在 cur 的左子树中< / span >
< a id = "__codelineno-27-17" name = "__codelineno-27-17" href = "#__codelineno-27-17" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span >
< a id = "__codelineno-27-18" name = "__codelineno-27-18" href = "#__codelineno-27-18" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-19" name = "__codelineno-27-19" href = "#__codelineno-27-19" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-20" name = "__codelineno-27-20" href = "#__codelineno-27-20" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若无待删除节点,则直接返回< / span >
< a id = "__codelineno-27-21" name = "__codelineno-27-21" href = "#__codelineno-27-21" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span >
< a id = "__codelineno-27-22" name = "__codelineno-27-22" href = "#__codelineno-27-22" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-23" name = "__codelineno-27-23" href = "#__codelineno-27-23" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 0 or 1< / span >
< a id = "__codelineno-27-24" name = "__codelineno-27-24" href = "#__codelineno-27-24" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-25" name = "__codelineno-27-25" href = "#__codelineno-27-25" > < / a > < span class = "w" > < / span > < span class = "c1" > // 当子节点数量 = 0 / 1 时, child = null / 该子节点< / span >
< a id = "__codelineno-27-26" name = "__codelineno-27-26" href = "#__codelineno-27-26" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "w" > < / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "p" > :< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-27" name = "__codelineno-27-27" href = "#__codelineno-27-27" > < / a > < span class = "w" > < / span > < span class = "c1" > // 删除节点 cur< / span >
< a id = "__codelineno-27-28" name = "__codelineno-27-28" href = "#__codelineno-27-28" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > cur< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "n" > root< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-29" name = "__codelineno-27-29" href = "#__codelineno-27-29" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > )< / span >
< a id = "__codelineno-27-30" name = "__codelineno-27-30" href = "#__codelineno-27-30" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-31" name = "__codelineno-27-31" href = "#__codelineno-27-31" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span >
< a id = "__codelineno-27-32" name = "__codelineno-27-32" href = "#__codelineno-27-32" > < / a > < span class = "w" > < / span > < span class = "n" > pre< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-33" name = "__codelineno-27-33" href = "#__codelineno-27-33" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-34" name = "__codelineno-27-34" href = "#__codelineno-27-34" > < / a > < span class = "w" > < / span > < span class = "c1" > // 若删除节点为根节点,则重新指定根节点< / span >
< a id = "__codelineno-27-35" name = "__codelineno-27-35" href = "#__codelineno-27-35" > < / a > < span class = "w" > < / span > < span class = "n" > root< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > child< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-36" name = "__codelineno-27-36" href = "#__codelineno-27-36" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-37" name = "__codelineno-27-37" href = "#__codelineno-27-37" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-38" name = "__codelineno-27-38" href = "#__codelineno-27-38" > < / a > < span class = "w" > < / span > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-27-39" name = "__codelineno-27-39" href = "#__codelineno-27-39" > < / a > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-40" name = "__codelineno-27-40" href = "#__codelineno-27-40" > < / a > < span class = "w" > < / span > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-27-41" name = "__codelineno-27-41" href = "#__codelineno-27-41" > < / a > < span class = "w" > < / span > < span class = "n" > TreeNode< / span > < span class = "o" > ?< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > right< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-42" name = "__codelineno-27-42" href = "#__codelineno-27-42" > < / a > < span class = "w" > < / span > < span class = "k" > while< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "k" > null< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-27-43" name = "__codelineno-27-43" href = "#__codelineno-27-43" > < / a > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "n" > left< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-44" name = "__codelineno-27-44" href = "#__codelineno-27-44" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-45" name = "__codelineno-27-45" href = "#__codelineno-27-45" > < / a > < span class = "w" > < / span > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-27-46" name = "__codelineno-27-46" href = "#__codelineno-27-46" > < / a > < span class = "w" > < / span > < span class = "k" > remove< / span > < span class = "p" > (< / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "p" > );< / span >
< a id = "__codelineno-27-47" name = "__codelineno-27-47" href = "#__codelineno-27-47" > < / a > < span class = "w" > < / span > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-27-48" name = "__codelineno-27-48" href = "#__codelineno-27-48" > < / a > < span class = "w" > < / span > < span class = "n" > cur< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "w" > < / span > < span class = "o" > =< / span > < span class = "w" > < / span > < span class = "n" > tmp< / span > < span class = "p" > .< / span > < span class = "n" > val< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-27-49" name = "__codelineno-27-49" href = "#__codelineno-27-49" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-27-50" name = "__codelineno-27-50" href = "#__codelineno-27-50" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -3007,25 +3054,30 @@
< a id = "__codelineno-28-32" name = "__codelineno-28-32" href = "#__codelineno-28-32" > < / a > < span class = "c1" > // 当子节点数量 = 0 / 1 时, child = null / 该子节点< / span >
< a id = "__codelineno-28-33" name = "__codelineno-28-33" href = "#__codelineno-28-33" > < / a > < span class = "kd" > let< / span > < span class = "nv" > child< / span > < span class = "p" > =< / span > < span class = "n" > cur< / span > < span class = "p" > ?.< / span > < span class = "kr" > left< / span > < span class = "o" > !=< / span > < span class = "kc" > nil< / span > < span class = "p" > ?< / span > < span class = "n" > cur< / span > < span class = "p" > ?.< / span > < span class = "kr" > left< / span > < span class = "p" > :< / span > < span class = "n" > cur< / span > < span class = "p" > ?.< / span > < span class = "kr" > right< / span >
< a id = "__codelineno-28-34" name = "__codelineno-28-34" href = "#__codelineno-28-34" > < / a > < span class = "c1" > // 删除节点 cur< / span >
< a id = "__codelineno-28-35" name = "__codelineno-28-35" href = "#__codelineno-28-35" > < / a > < span class = "k" > if< / span > < span class = "n" > pre< / span > < span class = "p" > ?.< / span > < span class = "kr" > left< / span > < span class = "p" > ===< / span > < span class = "n" > cur< / span > < span class = "p" > {< / span >
< a id = "__codelineno-28-36" name = "__codelineno-28-36" href = "#__codelineno-28-36" > < / a > < span class = "n" > pre< / span > < span class = "p" > ?.< / span > < span class = "kr" > left< / span > < span class = "p" > =< / span > < span class = "n" > child< / span >
< a id = "__codelineno-28-37" name = "__codelineno-28-37" href = "#__codelineno-28-37" > < / a > < span class = "p" > }< / span > < span class = "k" > else< / span > < span class = "p" > {< / span >
< a id = "__codelineno-28-38" name = "__codelineno-28-38" href = "#__codelineno-28-38" > < / a > < span class = "n" > pre< / span > < span class = "p" > ?.< / span > < span class = "kr" > right< / span > < span class = "p" > =< / span > < span class = "n" > child< / span >
< a id = "__codelineno-28-39" name = "__codelineno-28-39" href = "#__codelineno-28-39" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-28-40" name = "__codelineno-28-40" href = "#__codelineno-28-40" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-28-41" name = "__codelineno-28-41" href = "#__codelineno-28-41" > < / a > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-28-42" name = "__codelineno-28-42" href = "#__codelineno-28-42" > < / a > < span class = "k" > else< / span > < span class = "p" > {< / span >
< a id = "__codelineno-28-43" name = "__codelineno-28-43" href = "#__codelineno-28-43" > < / a > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-28-44" name = "__codelineno-28-44" href = "#__codelineno-28-44" > < / a > < span class = "kd" > var< / span > < span class = "nv" > tmp< / span > < span class = "p" > =< / span > < span class = "n" > cur< / span > < span class = "p" > ?.< / span > < span class = "kr" > right< / span >
< a id = "__codelineno-28-45" name = "__codelineno-28-45" href = "#__codelineno-28-45" > < / a > < span class = "k" > while< / span > < span class = "n" > tmp< / span > < span class = "p" > ?.< / span > < span class = "kr" > left< / span > < span class = "o" > !=< / span > < span class = "kc" > nil< / span > < span class = "p" > {< / span >
< a id = "__codelineno-28-46" name = "__codelineno-28-46" href = "#__codelineno-28-46" > < / a > < span class = "n" > tmp< / span > < span class = "p" > =< / span > < span class = "n" > tmp< / span > < span class = "p" > ?.< / span > < span class = "kr" > left< / span >
< a id = "__codelineno-28-47" name = "__codelineno-28-47" href = "#__codelineno-28-47" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-28-48" name = "__codelineno-28-48" href = "#__codelineno-28-48" > < / a > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-28-49" name = "__codelineno-28-49" href = "#__codelineno-28-49" > < / a > < span class = "n" > remove< / span > < span class = "p" > (< / span > < span class = "n" > num< / span > < span class = "p" > :< / span > < span class = "n" > tmp< / span > < span class = "p" > !.< / span > < span class = "n" > val< / span > < span class = "p" > )< / span >
< a id = "__codelineno-28-50" name = "__codelineno-28-50" href = "#__codelineno-28-50" > < / a > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-28-51" name = "__codelineno-28-51" href = "#__codelineno-28-51" > < / a > < span class = "n" > cur< / span > < span class = "p" > ?.< / span > < span class = "n" > val< / span > < span class = "p" > =< / span > < span class = "n" > tmp< / span > < span class = "p" > !.< / span > < span class = "n" > val< / span >
< a id = "__codelineno-28-52" name = "__codelineno-28-52" href = "#__codelineno-28-52" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-28-53" name = "__codelineno-28-53" href = "#__codelineno-28-53" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-28-35" name = "__codelineno-28-35" href = "#__codelineno-28-35" > < / a > < span class = "k" > if< / span > < span class = "n" > cur< / span > < span class = "o" > !=< / span > < span class = "n" > root< / span > < span class = "p" > {< / span >
< a id = "__codelineno-28-36" name = "__codelineno-28-36" href = "#__codelineno-28-36" > < / a > < span class = "k" > if< / span > < span class = "n" > pre< / span > < span class = "p" > ?.< / span > < span class = "kr" > left< / span > < span class = "p" > ===< / span > < span class = "n" > cur< / span > < span class = "p" > {< / span >
< a id = "__codelineno-28-37" name = "__codelineno-28-37" href = "#__codelineno-28-37" > < / a > < span class = "n" > pre< / span > < span class = "p" > ?.< / span > < span class = "kr" > left< / span > < span class = "p" > =< / span > < span class = "n" > child< / span >
< a id = "__codelineno-28-38" name = "__codelineno-28-38" href = "#__codelineno-28-38" > < / a > < span class = "p" > }< / span > < span class = "k" > else< / span > < span class = "p" > {< / span >
< a id = "__codelineno-28-39" name = "__codelineno-28-39" href = "#__codelineno-28-39" > < / a > < span class = "n" > pre< / span > < span class = "p" > ?.< / span > < span class = "kr" > right< / span > < span class = "p" > =< / span > < span class = "n" > child< / span >
< a id = "__codelineno-28-40" name = "__codelineno-28-40" href = "#__codelineno-28-40" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-28-41" name = "__codelineno-28-41" href = "#__codelineno-28-41" > < / a > < span class = "p" > }< / span > < span class = "k" > else< / span > < span class = "p" > {< / span >
< a id = "__codelineno-28-42" name = "__codelineno-28-42" href = "#__codelineno-28-42" > < / a > < span class = "c1" > // 若删除节点为根节点,则重新指定根节点< / span >
< a id = "__codelineno-28-43" name = "__codelineno-28-43" href = "#__codelineno-28-43" > < / a > < span class = "n" > root< / span > < span class = "p" > =< / span > < span class = "n" > cur< / span > < span class = "p" > ;< / span >
< a id = "__codelineno-28-44" name = "__codelineno-28-44" href = "#__codelineno-28-44" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-28-45" name = "__codelineno-28-45" href = "#__codelineno-28-45" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-28-46" name = "__codelineno-28-46" href = "#__codelineno-28-46" > < / a > < span class = "c1" > // 子节点数量 = 2< / span >
< a id = "__codelineno-28-47" name = "__codelineno-28-47" href = "#__codelineno-28-47" > < / a > < span class = "k" > else< / span > < span class = "p" > {< / span >
< a id = "__codelineno-28-48" name = "__codelineno-28-48" href = "#__codelineno-28-48" > < / a > < span class = "c1" > // 获取中序遍历中 cur 的下一个节点< / span >
< a id = "__codelineno-28-49" name = "__codelineno-28-49" href = "#__codelineno-28-49" > < / a > < span class = "kd" > var< / span > < span class = "nv" > tmp< / span > < span class = "p" > =< / span > < span class = "n" > cur< / span > < span class = "p" > ?.< / span > < span class = "kr" > right< / span >
< a id = "__codelineno-28-50" name = "__codelineno-28-50" href = "#__codelineno-28-50" > < / a > < span class = "k" > while< / span > < span class = "n" > tmp< / span > < span class = "p" > ?.< / span > < span class = "kr" > left< / span > < span class = "o" > !=< / span > < span class = "kc" > nil< / span > < span class = "p" > {< / span >
< a id = "__codelineno-28-51" name = "__codelineno-28-51" href = "#__codelineno-28-51" > < / a > < span class = "n" > tmp< / span > < span class = "p" > =< / span > < span class = "n" > tmp< / span > < span class = "p" > ?.< / span > < span class = "kr" > left< / span >
< a id = "__codelineno-28-52" name = "__codelineno-28-52" href = "#__codelineno-28-52" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-28-53" name = "__codelineno-28-53" href = "#__codelineno-28-53" > < / a > < span class = "c1" > // 递归删除节点 tmp< / span >
< a id = "__codelineno-28-54" name = "__codelineno-28-54" href = "#__codelineno-28-54" > < / a > < span class = "n" > remove< / span > < span class = "p" > (< / span > < span class = "n" > num< / span > < span class = "p" > :< / span > < span class = "n" > tmp< / span > < span class = "p" > !.< / span > < span class = "n" > val< / span > < span class = "p" > )< / span >
< a id = "__codelineno-28-55" name = "__codelineno-28-55" href = "#__codelineno-28-55" > < / a > < span class = "c1" > // 用 tmp 覆盖 cur< / span >
< a id = "__codelineno-28-56" name = "__codelineno-28-56" href = "#__codelineno-28-56" > < / a > < span class = "n" > cur< / span > < span class = "p" > ?.< / span > < span class = "n" > val< / span > < span class = "p" > =< / span > < span class = "n" > tmp< / span > < span class = "p" > !.< / span > < span class = "n" > val< / span >
< a id = "__codelineno-28-57" name = "__codelineno-28-57" href = "#__codelineno-28-57" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-28-58" name = "__codelineno-28-58" href = "#__codelineno-28-58" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >