Tree: Height of a Binary Tree

  • + 0 comments

    In Swift

    code gave run time because boilerplate code. Hackerrank must fix this line: " root = tree.insert(root: root, data: Int(readLine()!)!) ".

        func getHeight(root: Node?) -> Int {
            if root == nil {
                    return -1
            } else {
    
                    let left = getHeight(root: root?.left)
                    let right = getHeight(root: root?.right)
    
                    if left > right {
                            return left + 1
                    } else {
                            return right + 1
                    }
             }
        }