Skip to content

Instantly share code, notes, and snippets.

@feliperazeek
Created October 7, 2015 06:54
Show Gist options
  • Save feliperazeek/3833c7b2778cc8bb3efd to your computer and use it in GitHub Desktop.
Save feliperazeek/3833c7b2778cc8bb3efd to your computer and use it in GitHub Desktop.
Codility TreeHeight Scala
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