Problem Description
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.
Examples
Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3 Example 2: Input: root = [1,null,2] Output: 2
Python Solution
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
def maxDepth(root: Optional[TreeNode]) -> int:
if not root:
return 0
# Recursively find the depth of left and right subtrees
left_depth = maxDepth(root.left)
right_depth = maxDepth(root.right)
# Return the maximum depth plus 1 for current node
return max(left_depth, right_depth) + 1
Java Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
// Recursively find the depth of left and right subtrees
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
// Return the maximum depth plus 1 for current node
return Math.max(leftDepth, rightDepth) + 1;
}
}
C++ Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) {
return 0;
}
// Recursively find the depth of left and right subtrees
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
// Return the maximum depth plus 1 for current node
return max(leftDepth, rightDepth) + 1;
}
};
JavaScript Solution
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if (!root) {
return 0;
}
// Recursively find the depth of left and right subtrees
const leftDepth = maxDepth(root.left);
const rightDepth = maxDepth(root.right);
// Return the maximum depth plus 1 for current node
return Math.max(leftDepth, rightDepth) + 1;
};
C# Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int MaxDepth(TreeNode root) {
if (root == null) {
return 0;
}
// Recursively find the depth of left and right subtrees
int leftDepth = MaxDepth(root.left);
int rightDepth = MaxDepth(root.right);
// Return the maximum depth plus 1 for current node
return Math.Max(leftDepth, rightDepth) + 1;
}
}
Complexity Analysis
- Time Complexity: O(n) where n is the number of nodes in the tree
- Space Complexity: O(h) where h is the height of the tree
Solution Explanation
This solution uses recursive depth-first search:
- Key concept:
- Recursive traversal
- Compare depths
- Bottom-up approach
- Algorithm steps:
- Check base case
- Find left depth
- Find right depth
- Return maximum
Key points:
- Handle null root
- Recursive solution
- Simple implementation
- Efficient traversal