LeetCode Entry

104. Maximum Depth of Binary Tree

16.02.2023 easy 2023 kotlin

Let's write a one-liner.

104. Maximum Depth of Binary Tree easy

blog post

    fun maxDepth(root: TreeNode?): Int =
        root?.run { 1 + maxOf(maxDepth(left), maxDepth(right)) } ?: 0

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/120

Intuition

Do DFS and choose the maximum on each step.

Approach

Let’s write a one-liner.

Complexity

  • Time complexity: \(O(n)\)
  • Space complexity: \(O(log_2(n))\)