Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Minimum Depth of Binary Tree

Medium Acceptance 60.25% Points 30.00

You are given the root of a binary tree. Your task is to find its minimum depth — the number of nodes along the shortest path from the root node down to the nearest leaf node.

A leaf node is a node with no left or right child.

Return the minimum number of nodes present on any root-to-leaf path.

Examples
Example 1

image Input: root = [3,9,20,null,null,15,7]
Output: 2
Explanation: The shortest root-to-leaf path is 3 → 9, which contains 2 nodes.

Example 2

image Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
Explanation: The tree is skewed, so the only root-to-leaf path has 5 nodes.

Example 3

image Input: root = []
Output: 0
Explanation: An empty tree has a minimum depth of 0.

Constraints
    • 0 = Number of nodes = 105
    • -10? = Node.data = 10?
Companies
Microsoft Wayfair Nutanix
Topics
Tree Traversal Binary Tree
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions