Post

Trees: Maximum Depth of Binary Tree — Kotlin Solution

Trees: Maximum Depth of Binary Tree — Kotlin Solution

Problem Info

  
LeetCode #104 — Maximum Depth of Binary Tree
DifficultyEasy
TopicBinary Tree, DFS, BFS

Given the root of a binary tree, return its maximum depth. Maximum depth is the number of nodes along the longest path from the root down to the farthest leaf node.

Example:

1
2
3
4
5
6
7
    3
   / \
  9  20
    /  \
   15   7

Output: 3

The Node Definition

1
2
3
4
class TreeNode(var `val`: Int) {
    var left: TreeNode? = null
    var right: TreeNode? = null
}

Approach

The depth of a tree is:

1 + max(depth of left subtree, depth of right subtree)

At a null node, depth = 0. At a leaf, depth = 1. Recursion handles the rest naturally.

Walk through:

1
2
3
4
5
6
depth(3)  = 1 + max(depth(9), depth(20))
depth(9)  = 1 + max(0, 0) = 1
depth(20) = 1 + max(depth(15), depth(7))
depth(15) = 1, depth(7) = 1
depth(20) = 1 + max(1, 1) = 2
depth(3)  = 1 + max(1, 2) = 3 ✓

Kotlin Solution

Approach 1 — Recursive DFS (cleanest)

1
2
3
4
fun maxDepth(root: TreeNode?): Int {
    if (root == null) return 0
    return 1 + maxOf(maxDepth(root.left), maxDepth(root.right))
}

Approach 2 — Iterative BFS (level-by-level)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fun maxDepth(root: TreeNode?): Int {
    if (root == null) return 0

    val queue = ArrayDeque<TreeNode>()
    queue.add(root)
    var depth = 0

    while (queue.isNotEmpty()) {
        repeat(queue.size) {
            val node = queue.removeFirst()
            node.left?.let { queue.add(it) }
            node.right?.let { queue.add(it) }
        }
        depth++
    }

    return depth
}

Why Kotlin Shines Here

One-liner recursive solution — Kotlin makes the cleanest version possible:

1
2
3
4
return 1 + maxOf(maxDepth(root.left), maxDepth(root.right))
// The entire algorithm in one line
// vs Java: return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
// Kotlin's maxOf reads more naturally

repeat(queue.size) — process exactly one level at a time:

1
2
3
4
repeat(queue.size) {
    // processes all nodes at current depth before moving to next
}
// vs Java: int size = queue.size(); for (int i = 0; i < size; i++)

?.let — null-safe child addition:

1
2
node.left?.let { queue.add(it) }
// Only adds if non-null — no if (node.left != null) needed

Recursive vs Iterative BFS

ApproachTimeSpaceNotes
RecursiveO(n)O(h)Cleanest — one line
BFSO(n)O(w)w = max width of tree

h = height, w = max width. For a balanced tree, h = log n. For a skewed tree, h = n (recursive risks stack overflow). BFS is safer for very deep trees.


Complexity

  
TimeO(n) — visit every node
SpaceO(h) recursive / O(w) BFS

Key Takeaway

Depth = 1 + max(left depth, right depth). The recursive solution is a single return statement in Kotlin.

🔗 Solve it on LeetCode →


📚 Kotlin DSA Series

This post is part of the Kotlin DSA series — solving LeetCode problems using idiomatic Kotlin.

← View Full Series Index
This post is licensed under CC BY 4.0 by the author.