Given a binary tree containing n nodes. The problem is to get the sum of all the leaf nodes which are at minimum level in the binary tree.
Examples:
Input :
1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
Output : 11
Leaf nodes 4 and 7 are at minimum level.
Their sum = (4 + 7) = 11.
Source: Microsoft IDC Interview Experience | Set 150.
Approach: Perform iterative level order traversal using queue and find the first level containing a leaf node. Sum up all the leaf nodes at this level and then stop performing the traversal further.
// C++ implementation to find the sum of // leaf nodes at minimum level #include <bits/stdc++.h> using namespace std; // structure of a node of binary tree struct Node { int data; Node *left, *right; }; // function to get a new node Node* getNode(int data) { // allocate space Node* newNode = (Node*)malloc(sizeof(Node)); // put in the data newNode->data = data; newNode->left = newNode->right = NULL; return newNode; } // function to find the sum of // leaf nodes at minimum level int sumOfLeafNodesAtMinLevel(Node* root) { // if tree is empty if (!root) return 0; // if there is only one node if (!root->left && !root->right) return root->data; // queue used for level order traversal queue<Node*> q; int sum = 0; bool f = 0; // push root node in the queue 'q' q.push(root); while (f == 0) { // count number of nodes in the // current level int nc = q.size(); // traverse the current level nodes while (nc--) { // get front element from 'q' Node* top = q.front(); q.pop(); // if it is a leaf node if (!top->left && !top->right) { // accumulate data to 'sum' sum += top->data; // set flag 'f' to 1, to signify // minimum level for leaf nodes // has been encountered f = 1; } else { // if top's left and right child // exists, then push them to 'q' if (top->left) q.push(top->left); if (top->right) q.push(top->right); } } } // required sum return sum; } // Driver program to test above int main() { // binary tree creation Node* root = getNode(1); root->left = getNode(2); root->right = getNode(3); root->left->left = getNode(4); root->left->right = getNode(5); root->right->left = getNode(6); root->right->right = getNode(7); root->left->right->left = getNode(8); root->right->left->right = getNode(9); cout << "Sum = " << sumOfLeafNodesAtMinLevel(root); return 0; } |
Output:
Sum = 11
Time Complexity: O(n).
Auxiliary Space: O(n).
Recommended Posts:
- Difference between sums of odd level and even level nodes of a Binary Tree
- Connect Nodes at same Level (Level Order Traversal)
- Find depth of the deepest odd level leaf node
- Delete leaf nodes with value k
- Delete leaf nodes with value as x
- Sum of all leaf nodes of binary tree
- Print all nodes that are at distance k from a leaf node
- Count Non-Leaf nodes in a Binary Tree
- Product of all leaf nodes of binary tree
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print all leaf nodes of a Binary Tree from left to right
- Sum of nodes on the longest path from root to leaf node
- Pairwise Swap leaf nodes in a binary tree
- Program to count leaf nodes in 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 contribute@geeksforgeeks.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.



