알고리즘/Leetcode

[leetcode/kotlin] 104. Maximum Depth of Binary Tree

llollhh_ 2020. 12. 5. 00:41
/**
 * Example:
 * var ti = TreeNode(5)
 * var v = ti.`val`
 * Definition for a binary tree node.
 * class TreeNode(var `val`: Int) {
 *     var left: TreeNode? = null
 *     var right: TreeNode? = null
 * }
 */
class Solution {
    
    fun maxDepth(root: TreeNode?): Int {
        if (root == null) {
            return 0
        }
        
        return Math.max(maxDepth(root?.left), maxDepth(root?.right)) + 1
    }
    
}