algorithm - Maximum Depth of Binary Tree in scala -


i'm doing exercise on leetcode using scala. problem i'm working on "maximum depth of binary tree", means find maximum depth of binary tree.

i've passed code intellij, keep having compile error(type mismatch) when submitting solution in leetcode. here code, there problem or 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 here : line 17: error: type mismatch; line 24: error: type mismatch; know quite strange because have 13 lines of codes, didn't made mistakes, trust me ;)

this looks error specific of leetcode problem. assume you're referring https://leetcode.com/problems/maximum-depth-of-binary-tree/description/

perhaps you're not supposed re-implement data structure provide implementation maxdepth, i.e. treenode given. try this:

object solution {     def maxdepth(root: treenode): int = {         if (root == null) {             0         } else {             math.max(maxdepth(root.left), maxdepth(root.right)) + 1         }     } } 

this assumes treenode data structure 1 given in comment:

/**  * definition binary tree node.  * class treenode(var _value: int) {  *   var value: int = _value  *   var left: treenode = null  *   var right: treenode = null  * }  */ 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -