Maximum Depth of Binary Tree

Read the lesson → understand the pattern → then code. Tutor works offline (built-in teacher); optional Cursor/Gemini keys upgrade chat quality.

Topic refresher: Binary Trees — Recursion Native

Answer for a node from left + right children.

int maxDepth(TreeNode* root) {
    if (!root) return 0;
    return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
Binary Trees

Maximum Depth of Binary Tree

Easy
LeetCode
2/3
EasyBinary TreesLeetCode #104

Max Depth

1 + max(depth(left), depth(right)).

Examples

Example 1

Input

3 9 20 null null 15 7

Output

3
Example 2

Input

1 null 2

Output

2

Stuck? Take a nudge

C++17