Skip to content

Instantly share code, notes, and snippets.

@royki
Forked from feliperazeek/CodilityTreeHeight.scala
Created July 31, 2019 20:57
Show Gist options
  • Save royki/e65899eb372932c162b625c31781eec7 to your computer and use it in GitHub Desktop.
Save royki/e65899eb372932c162b625c31781eec7 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