I need to mirror binary tree in Scala. I think it have to work like this:
sealed trait BT[+A]
case object Empty extends BT[Nothing]
case class Node[+A](elem:A, left:BT[A], right:BT[A]) extends BT[A]
def mirrorTree[A](bt: BT[A]): BT[A] = bt match {
case Empty => Empty
case Node(root, left, right) => Node(root, right, left)
}
val t = Node(1,Node(2,Empty,Node(3,Empty,Empty)),Empty)
mirrorTree(t)
But it returns me only swapped first level, I have to call function in recursive way for left and right subtree, but I do not know how to do it if I have to return tree from function, so I can not use operators like for lists.