Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
Given a root node to a tree, find the sum of all the leaf nodes which are at maximum depth from root node.
Example:
1
/ \
2 3
/ \ / \
4 5 6 7
Input : root(of above tree)
Output : 22
Explanation:
Nodes at maximum depth are 4, 5, 6, 7.
So, the sum of these nodes = 22
Approach: There exists a recursive approach to this problem. This can also be solved using level order traversal and map. The idea is to do a traversal using a queue and keep track of current level. A map has been used to store the sum of nodes at the current level. Once all nodes are visited and the traversal is done, the last element of the map will contain the sum at the maximum depth of the tree.
Below is the implementation of the above approach:
C++
// C++ program to calculate the sum of // nodes at the maximum depth of a binary tree #include <bits/stdc++.h> using namespace std; struct node { int data; node *left, *right; } * temp; node* newNode(int data) { temp = new node; temp->data = data; temp->left = temp->right = NULL; return temp; } // Function to return the sum int SumAtMaxLevel(node* root) { // Map to store level wise sum. map<int, int> mp; // Queue for performing Level Order Traversal. // First entry is the node and // second entry is the level of this node. queue<pair<node*, int> > q; // Root has level 0. q.push({ root, 0 }); while (!q.empty()) { // Get the node from front of Queue. pair<node*, int> temp = q.front(); q.pop(); // Get the depth of current node. int depth = temp.second; // Add the value of this node in map. mp[depth] += (temp.first)->data; // Push children of this node, // with increasing the depth. if (temp.first->left) q.push({ temp.first->left, depth + 1 }); if (temp.first->right) q.push({ temp.first->right, depth + 1 }); } map<int, int>::iterator it; // Get the max depth from map. it = mp.end(); // last element it--; // return the max Depth sum. return it->second; } // Driver Code int main() { node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); cout << SumAtMaxLevel(root) << endl; return 0; } |
Python3
# Python3 program to calculate the # sum of nodes at the maximum depth # of a binary tree # Helper function that allocates a # new node with the given data and # None left and right poers. class newNode: # Constructor to create a new node def __init__(self, data): self.data = data self.left = None self.right = None # Function to return the sum def SumAtMaxLevel(root): # Map to store level wise sum. mp = {} # Queue for performing Level Order # Traversal. First entry is the node # and second entry is the level of # this node. q = [] # Root has level 0. q.append([root, 0]) while (len(q)): # Get the node from front # of Queue. temp = q[0] q.pop(0) # Get the depth of current node. depth = temp[1] # Add the value of this node in map. if depth not in mp: mp[depth] = 0 mp[depth] += (temp[0]).data # append children of this node, # with increasing the depth. if (temp[0].left) : q.append([temp[0].left, depth + 1]) if (temp[0].right) : q.append([temp[0].right, depth + 1]) # return the max Depth sum. return list(mp.values())[-1] # Driver Code if __name__ == '__main__': # Let us construct the Tree # shown in the above figure root = newNode(1) root.left = newNode(2) root.right = newNode(3) root.left.left = newNode(4) root.left.right = newNode(5) root.right.left = newNode(6) root.right.right = newNode(7) print(SumAtMaxLevel(root)) # This code is contributed by # Shubham Singh(SHUBHAMSINGH10) |
22
Recommended Posts:
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Sum of nodes at maximum depth of a Binary Tree
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check for Symmetric Binary Tree (Iterative Approach)
- Get level of a node in binary tree | iterative approach
- Iterative approach to check if a Binary Tree is Perfect
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Iterative approach to check for children sum property in a Binary Tree
- Deepest right leaf node in a binary tree | Iterative approach
- Deepest left leaf node in a binary tree | iterative approach
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Count full nodes in a Binary tree (Iterative and Recursive)
- Iterative program to count leaf nodes in a Binary Tree
- Count half nodes in a Binary tree (Iterative and Recursive)
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.
Improved By : SHUBHAMSINGH10



