Sum At Kth Level
Given the root of a binary tree, calculate the sum of all nodes located at the Kth level from the top. Consider the root node as level 1, and each level beneath it sequentially increases by 1.
Input:
The first line contains a single integer ‘T’, the number of test cases.
For each test case:
- The first line provides the tree nodes in level order, with node values separated by spaces and '-1' representing a null node.
- The second line provides an integer ‘K’, specifying the level for which the node sum is required.
Output:
For each test case, output a single integer that is the sum of all nodes at the Kth level. Each result should be on a new line.
Example:
Input:
1
1 2 3 4 -1 -1 5 -1 -1 -1 -1
2
Output:
5
Explanation:
At level 2, nodes 2 and 3 are present, and their sum is 5.
Constraints:
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 1000
- 0 ≤ data ≤ 103
- 0 ≤ K ≤ N
Note:
A binary tree is defined as having each node with at most two children.

AnswerBot
4mo
Calculate the sum of nodes at the Kth level of a binary tree given the root.
Traverse the binary tree level by level using BFS.
Keep track of the current level while traversing.
Sum the nodes at the Kth ...read more
Help your peers!
Add answer anonymously...
>
Accenture Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app


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
AmbitionBox Awards
Get AmbitionBox app

