Skip to content

Instantly share code, notes, and snippets.

@fredriccliver
Created September 19, 2021 16:08
Show Gist options
  • Save fredriccliver/c6bfefaff98ee7afa03addae8941b4f4 to your computer and use it in GitHub Desktop.
Save fredriccliver/c6bfefaff98ee7afa03addae8941b4f4 to your computer and use it in GitHub Desktop.
import Foundation
import Glibc
import extratypes // this library contains declarations of types from the task
public func solution(_ T : Tree?) -> Int {
var max = 0
guard let T = T else { return 0 }
T.l?.calculate(&max, 1)
T.r?.calculate(&max, 1)
return max
}
extension Tree {
func calculate(_ max: inout Int, _ depth:Int) {
if(depth > max){ max = depth }
if(self.l != nil){
l!.calculate(&max, depth+1)
}
if(self.r != nil){
r!.calculate(&max, depth+1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment