Problem
Problem page: https://leetcode.com/problems/minimum-depth-of-binary-tree/
Topics: Tree, Depth-first Search, Breadth-first Search
Difficulty: Easy
Similar problems: (E) Binary Tree Level Order Traversal, (E) Meeting Rooms, (E) Maximum Depth of Binary Tree
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Analysis
Find the minimum depth of a binary tree. I think there's another problem that asks for the maximum depth of a binary tree. The solutions are pretty similar: recursion with backtracking.
One thing to watch out for: if a node's left subtree has height 2 and its right subtree has height 0, that means the node has no right child. The minimum height of the subtree rooted at that node should be 2+1=3, not 0+1=1.
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-minimum-depth-of-binary-tree/
