Given a Binary Search Tree, the task is to find the horizontal sum of the nodes that are in the same level.
Approach: Find the height of the given binary tree then the number of levels in the tree will be levels = height + 1. Now create an array sum of size levels where sum[i] will store the sum of all the nodes at the ith level. In order to update this array, write a recursive function that add the current node’s data at sum[level] where level is the level of the current node and then recursively call the same method for the child nodes with level as level + 1.
Below is the implementation of the above approach:
6 12 24
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.
- Print all Co-Prime Levels of a Binary Tree
- Left-Right traversal of all the levels of Binary tree
- Print all nodes between two given levels in Binary Tree
- Binary Search Tree | Set 1 (Search and Insertion)
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Count the Number of Binary Search Trees present in a Binary Tree
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Binary Tree to Binary Search Tree Conversion
- Difference between Binary Tree and Binary Search Tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Optimal Binary Search Tree | DP-24
- Floor in Binary Search Tree (BST)
- Make Binary Search Tree
- Binary Search Tree | Set 2 (Delete)
- Inorder Successor in Binary Search Tree
- Print Binary Search Tree in Min Max Fashion
- Print all odd nodes of Binary Search Tree
- Number of pairs with a given sum in a Binary Search Tree
- Threaded Binary Search Tree | Deletion
- Iterative searching in Binary Search 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 email@example.com. 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.