Asked inWalmart,SDE-2

Maximum Depth of a Binary Tree Problem Statement

Given the root node of a binary tree with N nodes, where each node contains integer values, determine the maximum depth of the tree. The depth is defined as the longest path from the root node to the farthest leaf node.

Explanation:

Calculate the maximum number of nodes encountered from the root to the farthest leaf node, treating this as the tree's height.

Input:
The first line contains an integer T, the number of test cases.
The following lines contain integers representing the level order traversal of the tree (using -1 for null nodes).
Output:
For each test case, return an integer, indicating the maximum depth of the given binary tree.
Example:

Given a binary tree:

[5, 10, 15, 20, -1, 25, 30, -1, 40, -1, -1, -1, -1, 45]

The maximum depth can be determined by the path [5 -> 10 -> 25 -> 35 -> 40 -> 45 -> 50]. Therefore, the output is 7.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 3000
  • 0 ≤ data ≤ 10^9
  • Time Limit: 1 second

Note: You do not need to perform any input/output operations; focus on implementing the logic to solve the problem.

AnswerBot
4mo

Find the maximum depth of a binary tree given its level order traversal.

  • Traverse the tree level by level and keep track of the depth using a queue data structure.

  • For each level, increment the depth by...read more

Help your peers!
Select
Add answer anonymously...

Walmart SDE-2 interview questions & answers

A SDE-2 was asked Q. Bipartite Graph Problem Statement Determine if a given graph is bipartite. A gra...read more
A SDE-2 was asked Q. Can you explain the DHCP Protocol?
A SDE-2 was asked Q. Can you explain the TCP/IP protocol?

Popular interview questions of SDE-2

A SDE-2 was asked Q1. Bipartite Graph Problem Statement Determine if a given graph is bipartite. A gra...read more
A SDE-2 was asked Q2. Can you explain the DHCP Protocol?
A SDE-2 was asked Q3. Can you explain the TCP/IP protocol?
Walmart SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits