Maximum Depth of Binary Tree Visualizer & Step-by-Step Algorithm Solution

Find the maximum depth (height) of a binary tree by calculating 1 + max(leftDepth, rightDepth) recursively.

Category: trees | Difficulty: Easy

Tags: DFS, Binary Tree, Recursion, Tree Height

Maximum Depth of Binary Tree

3
9
20
15
7
100%
state
depth0
Initialization
1/23
Explanation

Start maxDepth on tree with root: Node(3).

Source Code
1function maxDepth(root) {
2 if (!root) return 0;
3 const leftDepth = maxDepth(root.left);
4 const rightDepth = maxDepth(root.right);
5 return 1 + Math.max(leftDepth, rightDepth);
6}