Minimum Depth of a Binary Tree

Determine the minimum depth of an integer-based binary tree. The minimum depth is defined as the number of nodes present along the shortest path from the root node down to the nearest leaf node.

Input:

The first line contains an integer 'T', denoting the number of test cases. Each test case comprises tree elements in level order form. Utilize -1 to denote a null node.

Output:

Output the minimum depth of the binary tree for each test case, each on a new line.

Example:

Input:
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
Output:
2
Explanation:

The shortest path is from the root node 1 to the leaf node 2, traversing 2 nodes, resulting in a minimum depth of 2.

Constraints:

  • 1 <= T <= 10^2
  • 0 <= N <= 10^3

Note: A leaf is defined as a node without children.

AnswerBot
1y

The minimum depth of a binary tree is the number of nodes along the shortest path from the root node down to the nearest leaf node.

  • The minimum depth can be found by performing a breadth-first search (...read more

Help your peers!
Add answer anonymously...
F5 Networks Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter