104. Maximum Depth of Binary Tree LeetCode Solution
In this guide, you will get 104. Maximum Depth of Binary Tree LeetCode Solution with the best time and space complexity. The solution to Maximum Depth of Binary Tree problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Depth of Binary Tree solution in C++
- Maximum Depth of Binary Tree solution in Java
- Maximum Depth of Binary Tree solution in Python
- Additional Resources

Problem Statement of Maximum Depth of Binary Tree
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.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 3
Example 2:
Input: root = [1,null,2]
Output: 2
Constraints:
The number of nodes in the tree is in the range [0, 104].
-100 <= Node.val <= 100
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
104. Maximum Depth of Binary Tree LeetCode Solution in C++
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root == nullptr)
return 0;
return 1 + max(maxDepth(root->left), maxDepth(root->right));
}
};
/* code provided by PROGIEZ */
104. Maximum Depth of Binary Tree LeetCode Solution in Java
class Solution {
public int maxDepth(TreeNode root) {
if (root == null)
return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
// code provided by PROGIEZ
104. Maximum Depth of Binary Tree LeetCode Solution in Python
class Solution:
def maxDepth(self, root: TreeNode | None) -> int:
if not root:
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.