KatInterview

104. Maximum Depth of Binary Tree

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the maximum depth of a binary tree. The maximum depth is defined as the number of nodes along the longest path from the root node to a leaf node. An empty tree (where the root is null) has a depth of 0. For a non-empty tree, its depth is determined by the longer of its two subtrees (left and right), plus one for the root node itself. This structure lends itself well to a recursive solution.

Solution

Maximum Depth of Binary Tree in Python

# 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
class Solution:
    def maxDepth(self, root: Optional[TreeNode]) -> int:
        # Base case: If the node is None, its depth is 0.
        if not root:
            return 0
        
        # Recursively calculate the depth of the left subtree.
        left_depth = self.maxDepth(root.left)
        # Recursively calculate the depth of the right subtree.
        right_depth = self.maxDepth(root.right)
        
        # The depth of the tree is 1 (for the current node) plus the maximum depth
        # of its left and right subtrees.
        return 1 + max(left_depth, right_depth)

Maximum Depth of Binary Tree in Java

/**
 * 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) {
        // Base case: If the node is null, its depth is 0.
        if (root == null) {
            return 0;
        }
        
        // Recursively calculate the depth of the left subtree.
        int leftDepth = maxDepth(root.left);
        // Recursively calculate the depth of the right subtree.
        int rightDepth = maxDepth(root.right);
        
        // The depth of the tree is 1 (for the current node) plus the maximum depth
        // of its left and right subtrees.
        return 1 + Math.max(leftDepth, rightDepth);
    }
}

Maximum Depth of Binary Tree in C++

/**
 * 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) {
        // Base case: If the node is null, its depth is 0.
        if (root == nullptr) {
            return 0;
        }
        
        // Recursively calculate the depth of the left subtree.
        int left_depth = maxDepth(root->left);
        // Recursively calculate the depth of the right subtree.
        int right_depth = maxDepth(root->right);
        
        // The depth of the tree is 1 (for the current node) plus the maximum depth
        // of its left and right subtrees.
        return 1 + std::max(left_depth, right_depth);
    }
};

Maximum Depth of Binary Tree in TypeScript

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function maxDepth(root: TreeNode | null): number {
    // Base case: If the node is null, its depth is 0.
    if (root === null) {
        return 0;
    }
    
    // Recursively calculate the depth of the left subtree.
    const leftDepth = maxDepth(root.left);
    // Recursively calculate the depth of the right subtree.
    const rightDepth = maxDepth(root.right);
    
    // The depth of the tree is 1 (for the current node) plus the maximum depth
    // of its left and right subtrees.
    return 1 + Math.max(leftDepth, rightDepth);
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.