15% off all plans & roadmaps Use code
See plansLeetCode 104 · Solution & editorial
Given the root of a binary tree, return its maximum depth.
Maximum Depth of Binary Tree is a Tree Traversal Patterns (DFS & BFS) problem, in the Recursive Postorder Traversal subpattern. The pattern covers preorder, inorder, level order traversal, LCA, and serialization
Master postorder traversal for tree height, diameter, and path sum problems. Process children before root.
The reference solution runs in O(n) time and O(h) space.
Maximum Depth of Binary Tree has been reported in interviews at LinkedIn, Amazon, Intel.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Maximum Depth of Binary Tree is a Tree Traversal Patterns (DFS & BFS) problem, in the Recursive Postorder Traversal subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.