       }
     } else if n == self.value {
       self.count -= 1
+      if self.count == 0 {
+        if let l = self.L, _ = self.R { // both
+          let maxValue = l.maximum().value
+          self.value = maxValue
+          self.count = 1
+          l.remove(maxValue)
+        } else if let l = self.L { // L only
+          self.parent?.replace(self, with: l)
+        } else if let r = self.R { // R only
+          self.parent?.replace(self, with: r)
+        } else {
+          self.parent?.replace(self, with: nil)
+        }
+      }
     } else {
       if let r = self.R {
         r.remove(n)

      if let l = self.L {
        l.insert(n)
      } else {
        self.L = Candy(n, self)
      }
    } else if n == self.value {
      self.count += 1

  return next
}

class BinaryTree {
  var value: Int
  var R: BinaryTree?
  var L: BinaryTree?
  var parent: BinaryTree?
  var count: Int

  init(_ v: Int, _ p: BinaryTree? = nil) {
    self.value = v
    self.count = 1
    self.parent = p
