-
-
Save royki/e65899eb372932c162b625c31781eec7 to your computer and use it in GitHub Desktop.
Codility TreeHeight Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Solution { | |
def solution(T: Tree): Int = { | |
def height(t: Tree, i: Int): Int = { | |
val left = (Option(t.l).map(height(_, i + 1)) getOrElse i) | |
val right = (Option(t.r).map(height(_, i + 1)) getOrElse i) | |
scala.math.max(i, scala.math.max(left, right)) | |
} | |
height(T, 0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment