Isaac Emmanuel
15 Jun 2024•EasyC++ / Python / TypeScript

Minimum Depth of Binary Tree

C++, Python and TypeScript solutions for Minimum Depth of Binary Tree.

The Minimum Depth of Binary Tree solutions are below.

Solution files

Python•minimum-depth-of-binary-tree/synced-solution.py
class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        if not root.left:
            return 1 + self.minDepth(root.right)
        if not root.right:
            return 1 + self.minDepth(root.left)
        return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
C++•minimum-depth-of-binary-tree/synced-solution.cpp
class Solution {
public:
    int minDepth(TreeNode* root) {
        if (!root) return 0;
        if (!root->left) return 1 + minDepth(root->right);
        if (!root->right) return 1 + minDepth(root->left);
        return 1 + min(minDepth(root->left), minDepth(root->right));
    }
};
TypeScript•minimum-depth-of-binary-tree/synced-solution.ts
function minDepth(root: TreeNode | null): number {
    if (!root) return 0;
    if (!root.left) return 1 + minDepth(root.right);
    if (!root.right) return 1 + minDepth(root.left);
    return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}