struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
#include <iostream>
#include <map>
#include <queue>
void bottomView(Node* root) {
if (!root) return;
std::map<int, int> hdNodeMap; // horizontal distance -> node data
std::queue<std::pair<Node*, int>> q;
q.push({root, 0});
while (!q.empty()) {
auto p = q.front(); q.pop();
Node* node = p.first;
int hd = p.second;
hdNodeMap[hd] = node->data;
if (node->left) q.push({node->left, hd - 1});
if (node->right) q.push({node->right, hd + 1});
}
for (auto& it : hdNodeMap) {
std::cout << it.second << " ";
}
}
int main() {
Node* root = new Node(20);
root->left = new Node(8);
root->right = new Node(22);
root->left->left = new Node(5);
root->left->right = new Node(3);
root->right->left = new Node(4);
root->right->right = new Node(25);
root->left->right->left = new Node(10);
root->left->right->right = new Node(14);
bottomView(root);
return 0;
}The bottom view prints the last node encountered at each horizontal distance during level order traversal.
Horizontal distances and nodes are:
- -2: 5
- -1: 10
- 0: 4
- 1: 14
- 2: 25
So output is: 5 10 4 14 25
Horizontal distance is the relative horizontal position of a node from the root.
Root has HD 0, left child HD -1, right child HD +1, and so on.
void bottomView(Node* root) {
if (!root) return;
std::map<int, int> hdNodeMap;
std::queue<std::pair<Node*, int>> q;
q.push({root, 0});
while (!q.empty()) {
auto p = q.front(); q.pop();
Node* node = p.first;
int hd = p.second;
if (hdNodeMap.find(hd) == hdNodeMap.end()) {
hdNodeMap[hd] = node->data;
}
if (node->left) q.push({node->left, hd - 1});
if (node->right) q.push({node->right, hd + 1});
}
for (auto& it : hdNodeMap) {
std::cout << it.second << " ";
}
}The code stores the node data only if the horizontal distance is not already in the map.
This means it keeps the first node encountered at that horizontal distance during level order traversal.
But bottom view requires the last node at that horizontal distance, so the map should be updated every time.
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
#include <iostream>
#include <map>
#include <queue>
void bottomView(Node* root) {
if (!root) return;
std::map<int, int> hdNodeMap;
std::queue<std::pair<Node*, int>> q;
q.push({root, 0});
while (!q.empty()) {
auto p = q.front(); q.pop();
Node* node = p.first;
int hd = p.second;
hdNodeMap[hd] = node->data;
if (node->left) q.push({node->left, hd - 1});
if (node->right) q.push({node->right, hd + 1});
}
for (auto& it : hdNodeMap) {
std::cout << it.second << " ";
}
}
int main() {
Node* root = new Node(1);
root->right = new Node(2);
root->right->right = new Node(5);
root->right->right->left = new Node(3);
root->right->right->right = new Node(6);
root->right->right->left->left = new Node(4);
bottomView(root);
return 0;
}Horizontal distances and nodes:
- 0: 4
- 1: 3
- 2: 5
- 3: 6
Bottom view prints nodes in order of HD: 4 3 5 6
BFS visits nodes level by level, so nodes at lower levels overwrite nodes at higher levels for the same horizontal distance.
This ensures the bottom-most node is recorded.