
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
10d

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...
Top Accenture Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Top HR questions asked in Accenture Software Developer Intern
>
Accenture Software Developer Intern Interview Questions
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