0
0
DSA C++programming~20 mins

Bottom View of Binary Tree in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bottom View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Bottom View for a Simple Binary Tree
What is the bottom view of the following binary tree after running the given code?
DSA C++
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;
}
A5 8 10 14 25
B5 8 3 14 25
C5 10 3 14 25
D5 10 4 14 25
Attempts:
2 left
💡 Hint
Remember bottom view shows the last node at each horizontal distance when viewed from bottom.
🧠 Conceptual
intermediate
1:00remaining
Horizontal Distance in Bottom View
In the bottom view of a binary tree, what does the horizontal distance (HD) represent?
AThe distance of the node from the root measured horizontally, left is negative, right is positive
BThe vertical level of the node in the tree
CThe depth of the node from the root
DThe number of nodes in the subtree rooted at that node
Attempts:
2 left
💡 Hint
Think about how nodes are positioned relative to the root when viewed from the side.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Bottom View Implementation
What is the bug in the following bottom view code snippet?
DSA C++
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 << " ";
    }
}
AIt does not check for null nodes before pushing to queue
BIt uses a queue instead of a stack, causing wrong traversal order
CIt only stores the first node at each horizontal distance, not the bottom-most node
DIt uses map which does not maintain order of horizontal distances
Attempts:
2 left
💡 Hint
Bottom view should show the last node at each horizontal distance, not the first.
Predict Output
advanced
2:00remaining
Bottom View Output for Unbalanced Tree
What is the output of the bottom view function for this unbalanced binary tree?
DSA C++
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;
}
A4 3 5 6
B1 2 3 5 6
C1 2 5 3 6
D1 2 5 4 6
Attempts:
2 left
💡 Hint
Track horizontal distances carefully for each node.
🧠 Conceptual
expert
1:30remaining
Why Use Level Order Traversal for Bottom View?
Why is level order traversal (BFS) preferred over DFS for computing the bottom view of a binary tree?
ABecause DFS cannot track horizontal distances correctly
BBecause BFS processes nodes level by level ensuring the bottom-most nodes overwrite previous ones at the same horizontal distance
CBecause BFS uses less memory than DFS in all cases
DBecause DFS visits nodes in random order which is unsuitable for bottom view
Attempts:
2 left
💡 Hint
Think about how nodes at the same horizontal distance but different depths are processed.