Created
September 19, 2021 16:08
-
-
Save fredriccliver/c6bfefaff98ee7afa03addae8941b4f4 to your computer and use it in GitHub Desktop.
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
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