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
- Print all nodes between two given levels in Binary Tree
- Binary Search Tree | Set 1 (Search and Insertion)
- Count the Number of Binary Search Trees present in a Binary Tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Binary Tree to Binary Search Tree Conversion
- Make Binary Search Tree
- Binary Search Tree | Set 2 (Delete)
- Optimal Binary Search Tree | DP-24
- Floor in Binary Search Tree (BST)
- Print Binary Search Tree in Min Max Fashion
- Print all even 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
- Inorder Successor 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 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.