I'm doing the exercise on leetcode using Scala. The problem I'm working on is "Maximum Depth of Binary Tree", which means find the maximum depth of a binary tree.
I've passed my code with IntelliJ, but I keep having compile error(type mismatch) when submitting my solution in Leetcode. Here is my code, is there any problem or any other solution please?
object Solution {
abstract class BinTree
case object EmptyTree extends BinTree
case class TreeNode(mid: Int, left: BinTree, right: BinTree) extends BinTree
def maxDepth(root: BinTree): Int = {
root match {
case EmptyTree => 0
case TreeNode(_, l, r) => Math.max(maxDepth(l), maxDepth(r)) + 1
}
}
}
The error is here : Line 17: error: type mismatch; Line 24: error: type mismatch; I know it is quite strange because I just have 13 lines of codes, but I didn't made mistakes, trust me ;)
TreeNodeis defined for you. You're stepping on the supplied code by trying to redefine it.