Loading problem…
You're building a feature that needs to calculate the maximum depth of a nested data structure or tree. This is useful for validating nested forms, calculating DOM tree depth, or analyzing hierarchical data.
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
// Tree: 3
// / \
// 9 20
// / \
// 15 7
maxDepth(root); // 3
// Tree: 1
maxDepth(root); // 1
// Tree: null
maxDepth(null); // 0This problem models real frontend scenarios: