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...
Top Walmart SDE-2 interview questions & answers
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app