Measure the Shortest Root-to-Leaf Node Path

Quick Overview

Find the shortest root-to-leaf node count by visiting binary-tree levels until the first actual leaf.

Measure the Shortest Root-to-Leaf Node Path

Company: TCS

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem Return the minimum depth of a binary tree: the number of nodes on its shortest root-to-leaf path. A leaf has neither a left nor a right child. A missing child of an internal node does not end a path. Return 0 for an empty tree. ```cpp int minDepth(TreeNode* root); ``` `TreeNode` contains `val`, `left`, and `right`; example brackets show level-order structure. ### Examples ```text Input: root = [9,4,12,null,6] Output: 2 ``` Node 12 is a leaf two nodes away from the root when counting both endpoints. ```text Input: root = [3,null,5,null,8,null,13] Output: 4 ``` ### Constraints - The tree has between 0 and 100000 nodes. - Values are between -1000 and 1000, inclusive.

Overview: Find the shortest root-to-leaf node count by visiting binary-tree levels until the first actual leaf.

TCS logo
TCS
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the minimum depth of a binary tree: the number of nodes on its shortest root-to-leaf path. A leaf has neither a left nor a right child. A missing child of an internal node does not end a path. Return 0 for an empty tree.

int minDepth(TreeNode* root);

TreeNode contains val, left, and right; example brackets show level-order structure.

Examples

Input: root = [9,4,12,null,6]
Output: 2

Node 12 is a leaf two nodes away from the root when counting both endpoints.

Input: root = [3,null,5,null,8,null,13]
Output: 4

Constraints

  • The tree has between 0 and 100000 nodes.
  • Values are between -1000 and 1000, inclusive.