Back to DSA

Maximum Depth of Binary Tree

easy
Acceptance: 70%
Binary Tree

Given the root of a binary tree, compute its maximum depth. The maximum depth is the number of nodes on the longest path from the root down to the most distant leaf.

Examples

Example 1:
Input:root = [5,3,8,1,4,null,10]
Output:3
Example 2:
Input:root = [2,null,7]
Output:2

Hints

00:00
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }
}
Time complexityO(n)
Space complexityO(h)