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.

Be the first one to answer
Add answer anonymously...
Walmart SDE-2 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