Sum of nodes in bottom view of Binary Tree
Given a binary tree, the task is to print the sum of nodes in bottom view of the given Binary Tree. Bottom view of a binary tree is the set of nodes visible when the tree is viewed from the bottom.
Examples:
Input :
1
/ \
2 3
/ \ \
4 5 6
Output : 20
Input :
1
/ \
2 3
\
4
\
5
\
6
Output : 17
Approach: The idea is to use a queue.
- Put tree nodes in a queue for the level order traversal
- Start with the horizontal distance(hd) 0 of the root node, keep on adding left child to queue along with the horizontal distance as hd-1 and right child as hd+1.
- Use a map to store the hd value(as key) and the last node(as pair) having the corresponding hd value.
- Every time, we encounter a new horizontal distance or an existing horizontal distance put the node data for the horizontal distance as key. For the first time it will add to the map, next time it will replace the value. This will make sure that the bottom most element for that horizontal distance is present in the map.
- Finally, traverse the map and calculate the sum of all of the elements.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Structure of binary tree struct Node { Node* left; Node* right; int hd; int data; }; // Function to create a new node Node* newNode(int key) { Node* node = new Node(); node->left = node->right = NULL; node->data = key; return node; } // Function that returns the sum of // nodes in bottom view of binary tree int SumOfBottomView(Node* root) { if (root == NULL) return 0; queue<Node*> q; map<int, int> m; int hd = 0; root->hd = hd; int sum = 0; // Push node and horizontal distance to queue q.push(root); while (q.size()) { Node* temp = q.front(); q.pop(); hd = temp->hd; // Put the dequeued tree node to Map // having key as horizontal distance. Every // time we find a node having same horizontal // distance we need to replace the data in // the map. m[hd] = temp->data; if (temp->left) { temp->left->hd = hd - 1; q.push(temp->left); } if (temp->right) { temp->right->hd = hd + 1; q.push(temp->right); } } map<int, int>::iterator itr; // Sum up all the nodes in bottom view of the tree for (itr = m.begin(); itr != m.end(); itr++) sum += itr->second; return sum; } // Driver Code int main() { Node* root = newNode(20); root->left = newNode(8); root->right = newNode(22); root->left->left = newNode(5); root->left->right = newNode(3); root->right->left = newNode(4); root->right->right = newNode(25); root->left->right->left = newNode(10); root->left->right->right = newNode(14); cout << SumOfBottomView(root); return 0; } |
Python3
# Python3 implementation of the approach
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
self.hd = None
# Function that returns the Sum of
# nodes in bottom view of binary tree
def SumOfBottomView(root):
if root == None:
return 0
q = []
m = {}
hd, Sum = 0, 0
root.hd = hd
# Push node and horizontal
# distance to queue
q.append(root)
while len(q) > 0:
temp = q.pop(0)
hd = temp.hd
# Put the dequeued tree node to Map
# having key as horizontal distance.
# Every time we find a node having
# same horizontal distance we need
# to replace the data in the map.
m[hd] = temp.data
if temp.left != None:
temp.left.hd = hd – 1
q.append(temp.left)
if temp.right != None:
temp.right.hd = hd + 1
q.append(temp.right)
# Sum up all the nodes in bottom
# view of the tree
for itr in m:
Sum += m[itr]
return Sum
# Driver Code
if __name__ == “__main__”:
root = Node(20)
root.left = Node(8)
root.right = Node(22)
root.left.left = Node(5)
root.left.right = Node(3)
root.right.left = Node(4)
root.right.right = Node(25)
root.left.right.left = Node(10)
root.left.right.right = Node(14)
print(SumOfBottomView(root))
# This code is contributed by Rituraj Jain
58
Recommended Posts:
- Bottom View of a Binary Tree
- Print Bottom-Right View of a Binary Tree
- Sum of nodes in top view of binary tree
- Print nodes in top view of Binary Tree | Set 2
- Print nodes in the Top View of Binary Tree | Set 3
- Print Nodes in Top View of Binary Tree
- Right view of Binary Tree using Queue
- Print Right View of a Binary Tree
- Print Left View of a Binary Tree
- Iterative Method To Print Left View of a Binary Tree
- Sum of all nodes in a binary tree
- Sink Odd nodes in Binary Tree
- XOR of path between any two nodes in a Binary Tree
- Sum of all leaf nodes of binary tree
- Product of all 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.
Improved By : rituraj_jain



