Leetcode 111. Minimum Depth of Binary Tree

Problem Explanation:

Given a binary tree, our task is to find the shortest path from the root node to any leaf (a node with no children).

Let's consider the example provided above:


9 20 /
15 7

We start at the root node 3 and choose either the left path (which leads to leaf node 9) or right path (which leads to the leaf nodes 15 and 7). Since the left path has the shortest distance (from 3 to 9) we select this as the minimum depth.

Approach Explanation:

The solution involves recursively traversing through the binary tree nodes.

  • If the root node is null, we return 0.

  • If the root's left child node is null, it means that the path with the smallest depth would be through the right child node. So, we return the minimum depth of right child node + 1.

  • Similarly, if the root's right child node is null, it means that the path with the smallest depth would be through the left child node. So, we return the minimum depth of left child node + 1.

  • If both the left and right child nodes are not null, it means that the path with the smallest depth could be through either nodes. So, we return the minimum depth of both left and right child nodes + 1.

Python Solution:

3class Solution:
4    def minDepth(self, root):
5        if not root:
6            return 0
7        if root.left is None:
8            return self.minDepth(root.right) + 1
9        elif root.right is None:
10            return self.minDepth(root.left) + 1
11        else:
12            return min(self.minDepth(root.left), self.minDepth(root.right)) + 1

Java Solution:

3class Solution {
4    public int minDepth(TreeNode root) {
5        if (root == null) return 0;
6        if (root.left == null) return minDepth(root.right) + 1;
7        if (root.right == null) return minDepth(root.left) + 1;
8        return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
9    }

JavaScript Solution:

3class Solution {
4    minDepth(root) {
5        if (root === null)
6            return 0;
7        if (root.left === null)
8            return this.minDepth(root.right) + 1;
9        if (root.right === null)
10            return this.minDepth(root.left) + 1;
11        return Math.min(this.minDepth(root.left), this.minDepth(root.right)) + 1;
12    }

C++ Solution:

3class Solution {
5    int minDepth(TreeNode* root) {
6        if (root == nullptr)
7            return 0;
8        if (root->left == nullptr)
9            return minDepth(root->right) + 1;
10        if (root->right == nullptr)
11            return minDepth(root->left) + 1;
12        return min(minDepth(root->left), minDepth(root->right)) + 1;
13    }

C# Solution:

3public class Solution {
4    public int MinDepth(TreeNode root) {
5        if (root == null)
6            return 0;
7        if (root.left == null)
8             return MinDepth(root.right) + 1;
9        if (root.right == null)
10            return MinDepth(root.left) + 1;
11        return Math.Min(MinDepth(root.left), MinDepth(root.right)) + 1;
12    }

Each of these solutions employ depth-first search (DFS), a popular tree/graph traversal algorithm, to achieve the desired result.Occupying a time complexity of O(n) and space complexity of O(n), these solution algorithms fit the best for large datasets for calculating minimum depths of binary trees.

In conclusion, when encountered with tasks of finding minimum depths in binary trees, employing recursive traversing of nodes and depth-first search (DFS) algorithm can serve as an efficient and optimal approach. It enables you to traverse the tree in depth and select the shortest route. Be it Python, Java, JavaScript, C++, or C#, each of these programming languages are capable of executing tasks of this nature, providing ideal results.

While the problem seems complex on the surface, its solution is rather simple and effective, making it an interesting problem within the field of data structures and algorithms. Understanding the logic behind tree traversal and recursive function calls can prove beneficial while tackling such problems in fields like machine learning, artificial intelligence, software development, or even computer graphics where tree structures are commonly used.

Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

TA 👨‍🏫