Given a binary tree with N nodes and an integer K, the task is to find the sum of all the nodes present at the Kth level.
- Traverse the Binary Tree using Level Order Traversal and queue
- During traversal, pop each element out of the queue and push it’s child (if available) in the queue.
- Keep the track of the current level of the Binary tree.
- To track the current level, declare a variable level and increase it whenever a child is traversed from the parent.
- When the current level of the tree i.e. the variable level meets the required Kth level, pop the elements from the queue and calculate their sum.
Below is the implementation of the above approach:
Time Complexity: O(N)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Difference between sums of odd level and even level nodes of a Binary Tree
- Swap Nodes in Binary tree of every k'th level
- Nodes at Kth level without duplicates in a Binary Tree
- Print nodes between two given level numbers of a binary tree
- Count nodes with two children at level L in a Binary Tree
- Print all nodes except rightmost node of every level of the Binary Tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Difference between sums of odd position and even position nodes for each level of a Binary Tree
- Level with maximum number of nodes using DFS in a N-ary tree
- Sum of nodes at k-th level in a tree represented as string
- Count the number of nodes at given level in a tree using BFS.
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.