Deepest Leaves Sum Problem Statement
Given a binary tree of integers, your task is to calculate the sum of all the leaf nodes present at the deepest level of this binary tree. If there are no such nodes, output 0.
Input:
The first line contains an integer 'T' denoting the number of test cases.
Each test case includes one line of elements in level order form, with node values separated by a single space. A null node is represented by -1.
Output:
For each test case, output the sum of all leaf nodes at the deepest level, printed on a new line.
Example:
Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
7
Explanation:
The binary tree represented by the input has depth levels as depicted in a given image, with the deepest leaves being [7], thus the sum is 7.
Constraints:
- 1 ≤ T ≤ 102
- 0 ≤ N ≤ 3000
- The sum is guaranteed to fit within a 32-bit integer.
- Time limit: 1 second.
Note:
You do not need to handle printing the output in your submission. Focus on implementing the function to solve the problem.
AnswerBot
2d
Calculate the sum of leaf nodes at the deepest level of a binary tree.
Traverse the binary tree to find the deepest level of leaf nodes.
Sum the leaf nodes at the deepest level.
Handle null nodes represe...read more
Help your peers!
Add answer anonymously...
Top Walmart Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Walmart Software Developer
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