2 Solution in SWIFT: 1 liner and 99% top performance.


  • 0
    I

    1 Liner solution

    class Solution {
        func maxDepth(_ root: TreeNode?) -> Int {
            return root == nil ? 0 : max(maxDepth(root?.left), maxDepth(root?.right)) + 1
        }
    }
    

    Top performance solution for Swift at 99%:

    class Solution {
        func maxDepth(_ root: TreeNode?) -> Int {
            guard let root = root else { return 0 }
            let leftHeight = maxDepth(root.left) + 1
            let rightHeight = maxDepth(root.right) + 1
            return max(leftHeight, rightHeight)
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.