When removing a node from a binary search tree, you either replace the node with its biggest child on the left or its smallest child on the right.
I'm having a hard time understanding the way the following 
You call remove on the orange node:
selfis orange.replacementis the minimum of the right subtree - the blue node.
You recurse by calling remove on blue:
selfis bluereplacementis nil as blue has neither left nor right children- You call
removeonnil(or rather you don't, because of the ?) - Similarly, all of the other assignments are skipped because the optional values are all
nil reconnectParent(node: nil)is called to fix the left/right linkage of blue's parent (this will result ingreen.leftbeing assignednil)- You return blue
The tree now looks like:
You are now back out at the initial call to remove and execution continues. Remember,
selfis orange.replacementis blue node.
The next steps are:
blue.right = orange.rightblue.left = orange.leftorange.right.parent = blueorange.left.parent = blueorange.reconnectParentTo(node:blue)- This does nothing sinceorangehas no parentorange.parent = nilorange.right = nilorange.left = nil- Return from
remove- This was the initial call toremove, so we have exited all recursion
This leaves us with the final tree:


