LeetCode Entry
104. Maximum Depth of Binary Tree
Let's write a one-liner.
104. Maximum Depth of Binary Tree easy
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))\)