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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Nodes at Kth level without duplicates in a Binary Tree
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Prime Numbers present at Kth level of a Binary Tree
- Find the numbers present at Kth level of a Fibonacci Binary Tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Print all nodes except rightmost node of every level of the Binary Tree
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Connect Nodes at same Level (Level Order Traversal)
- Kth ancestor of all nodes in an N-ary tree using DFS
- Print nodes between two given level numbers of a binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Swap Nodes in Binary tree of every k'th level
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Count nodes with two children at level L in a Binary Tree
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Print even positioned nodes of even levels in level order of the given binary tree
- Difference between sums of odd position and even position nodes for each level of a Binary Tree
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.