Created
October 7, 2015 06:54
-
-
Save feliperazeek/3833c7b2778cc8bb3efd 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