Bird
Raised Fist0
DSA C++programming~10 mins

Bottom View of Binary Tree in DSA C++ - Execution Trace

Choose your learning style10 modes available
Concept Flow - Bottom View of Binary Tree
Start at root node
↓
Assign horizontal distance = 0
↓
Use queue for level order traversal
↓
For each node dequeued:
↓
Update map with node at HD
↓
Enqueue right child with HD+1
↓
Repeat until queue empty
↓
Extract bottom view from map sorted by HD
↓
Print bottom view nodes left to right
Traverse the tree level by level, track horizontal distances, update bottom-most node at each horizontal distance, then print nodes from leftmost to rightmost horizontal distance.
Execution Sample
DSA C++
struct Node {
  int data;
  Node* left;
  Node* right;
};

// Bottom view function uses level order traversal with HD tracking
This code traverses the binary tree level-wise, tracks horizontal distances, and updates the bottom view nodes.
Execution Table
StepOperationNode Visited (data)Horizontal Distance (HD)Map State (HD:Node)Queue State (Node, HD)Visual State
1Start at root200{0:20}[(20,0)]20(HD=0)
2Dequeue node200{0:20}[]20(HD=0)
3Enqueue left child8-1{0:20}[(8,-1)]20(0) -> 8(-1)
4Enqueue right child221{0:20}[(8,-1),(22,1)]8(-1) <- 20(0) -> 22(1)
5Dequeue node8-1{0:20, -1:8}[(22,1)]8(-1) -> 20(0) -> 22(1)
6Enqueue left child5-2{0:20, -1:8}[(22,1),(5,-2)]5(-2) -> 8(-1) -> 20(0) -> 22(1)
7Enqueue right child30{0:20, -1:8}[(22,1),(5,-2),(3,0)]5(-2) -> 8(-1) -> 3(0) -> 20(0) -> 22(1)
8Dequeue node221{0:20, -1:8, 1:22}[(5,-2),(3,0)]5(-2) -> 8(-1) -> 3(0) -> 20(0) -> 22(1)
9Enqueue right child252{0:20, -1:8, 1:22}[(5,-2),(3,0),(25,2)]5(-2) -> 8(-1) -> 3(0) -> 20(0) -> 22(1) -> 25(2)
10Dequeue node5-2{0:20, -1:8, 1:22, -2:5}[(3,0),(25,2)]5(-2) -> 8(-1) -> 3(0) -> 20(0) -> 22(1) -> 25(2)
11Dequeue node30{0:3, -1:8, 1:22, -2:5}[(25,2)]5(-2) -> 8(-1) -> 3(0) -> 22(1) -> 25(2)
12Dequeue node252{0:3, -1:8, 1:22, -2:5, 2:25}[]5(-2) -> 8(-1) -> 3(0) -> 22(1) -> 25(2)
13Queue empty--{-2:5, -1:8, 0:3, 1:22, 2:25}[]Bottom view nodes: 5 -> 8 -> 3 -> 22 -> 25
💡 Queue is empty, traversal complete, bottom view map finalized.
Variable Tracker
VariableStartAfter Step 2After Step 5After Step 8After Step 11Final
Queue[(20,0)][][(22,1)][(5,-2),(3,0)][(25,2)][]
Map (HD:Node){}{0:20}{0:20, -1:8}{0:20, -1:8, 1:22}{0:3, -1:8, 1:22, -2:5}{-2:5, -1:8, 0:3, 1:22, 2:25}
Current Node20822325-
Key Moments - 3 Insights
Why does the map update the node at horizontal distance 0 from 20 to 3 at step 11?
Because node 3 is lower (visited later in level order) at the same horizontal distance 0, it replaces 20 in the map as the bottom view node at HD 0 (see execution_table step 11).
Why do we use horizontal distance (HD) to track nodes?
HD helps identify vertical alignment of nodes. Nodes with the same HD are in the same vertical line. The bottom view shows the lowest node at each HD (see concept_flow and execution_table).
Why do we use a queue for traversal?
Queue enables level order traversal, visiting nodes top to bottom and left to right, ensuring bottom-most nodes overwrite earlier ones in the map (see execution_table steps showing queue state).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 7, what nodes are currently in the queue?
A[(22,1), (5,-2), (3,0)]
B[(8,-1), (22,1), (5,-2)]
C[(5,-2), (3,0)]
D[(3,0), (25,2)]
💡 Hint
Check the 'Queue State' column at step 7 in the execution_table.
At which step does the map first get updated with horizontal distance -2?
AStep 6
BStep 5
CStep 10
DStep 9
💡 Hint
Look at the 'Map State' column for when HD -2 appears in the execution_table.
If the node with data 3 was not present, what would be the bottom view node at HD 0 after traversal?
A22
B20
C8
D5
💡 Hint
Refer to the map updates in execution_table steps 11 and 12 where node 3 replaces 20 at HD 0.
Concept Snapshot
Bottom View of Binary Tree:
- Traverse tree level order with queue
- Track horizontal distance (HD) for each node
- Update map with latest node at each HD
- After traversal, print nodes sorted by HD
- Shows lowest visible nodes from bottom
- Uses map and queue for O(n) time
Full Transcript
The bottom view of a binary tree shows the nodes visible when the tree is viewed from the bottom. We start at the root with horizontal distance zero. Using a queue, we traverse the tree level by level. For each node, we record its horizontal distance. We update a map to keep the latest node at each horizontal distance, which represents the bottom-most node. We enqueue left and right children with horizontal distances decremented and incremented respectively. After the queue is empty, the map contains the bottom view nodes. We print these nodes sorted by their horizontal distance from left to right. This approach ensures we see the lowest nodes at each vertical line of the tree.

Practice

(1/5)
1. What does the bottom view of a binary tree represent?
easy
A. Nodes visible when the tree is viewed from the bottom
B. Nodes visible when the tree is viewed from the top
C. Nodes visible when the tree is viewed from the left side
D. Nodes visible when the tree is viewed from the right side

Solution

  1. Step 1: Understand the bottom view concept

    The bottom view shows nodes visible when looking from the bottom of the tree, meaning the lowest nodes at each horizontal distance.
  2. Step 2: Compare with other views

    Top view shows nodes visible from above, left and right views show nodes from sides, so bottom view is unique for bottom visibility.
  3. Final Answer:

    Nodes visible when the tree is viewed from the bottom -> Option A
  4. Quick Check:

    Bottom view = nodes visible from bottom [OK]
Hint: Bottom view shows lowest nodes at each horizontal distance [OK]
Common Mistakes:
  • Confusing bottom view with top view
  • Thinking bottom view shows all leaf nodes
  • Mixing bottom view with side views
2. Which of the following C++ STL containers is most suitable to store nodes by their horizontal distance for bottom view calculation?
easy
A. std::vector<int>
B. std::map
C. std::stack<int>
D. std::queue<int>

Solution

  1. Step 1: Identify data structure needs

    We need to map horizontal distances (int) to node values (int) and keep them sorted by horizontal distance.
  2. Step 2: Choose container

    std::map keeps keys sorted and allows key-value pairs, perfect for horizontal distance mapping.
  3. Final Answer:

    std::map -> Option B
  4. Quick Check:

    Use map for sorted key-value pairs [OK]
Hint: Use map to keep horizontal distances sorted [OK]
Common Mistakes:
  • Using vector which is index-based, not key-based
  • Using stack or queue which don't map keys to values
  • Not sorting horizontal distances
3. What is the bottom view output of the following binary tree?

        20
       /  \
     8     22
    / \      \
   5   3      25
      /  \    
     10   14
medium
A. [5, 8, 10, 14, 25]
B. [5, 10, 14, 22, 25]
C. [5, 10, 3, 14, 25]
D. [8, 10, 3, 14, 22]

Solution

  1. Step 1: Assign horizontal distances (HD) to nodes

    Root 20 at HD=0; 8 at HD=-1; 22 at HD=1; 5 at HD=-2; 3 at HD=0; 25 at HD=2; 10 at HD=-1; 14 at HD=1.
  2. Step 2: Determine bottom view nodes by HD

    At HD=-2: 5; HD=-1: 10 (lower than 8); HD=0: 3 (lower than 20); HD=1: 14 (lower than 22); HD=2: 25.
  3. Final Answer:

    [5, 10, 3, 14, 25] -> Option C
  4. Quick Check:

    Bottom view nodes by HD = [5,10,3,14,25] [OK]
Hint: Track horizontal distances and pick lowest nodes [OK]
Common Mistakes:
  • Choosing top nodes instead of bottom nodes
  • Mixing horizontal distances incorrectly
  • Ignoring nodes at same horizontal distance but lower level
4. Consider this code snippet for bottom view calculation. What is the error?

void bottomView(Node* root) {
  if (!root) return;
  std::map hdNodeMap;
  std::queue> 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 << " ";
  }
}
medium
A. It stores only the first node at each horizontal distance, not the bottom-most
B. It uses queue incorrectly for BFS traversal
C. It does not check for null root
D. It prints nodes in reverse horizontal order

Solution

  1. Step 1: Analyze node storage logic

    The code stores node data only if horizontal distance is not already in map, so it keeps first node encountered at that HD.
  2. Step 2: Understand bottom view requirement

    Bottom view requires the lowest node at each HD, so we must update map with nodes at lower levels, not just first.
  3. Final Answer:

    It stores only the first node at each horizontal distance, not the bottom-most -> Option A
  4. Quick Check:

    Bottom view needs last node at HD, not first [OK]
Hint: Update map with latest node at each horizontal distance [OK]
Common Mistakes:
  • Not updating map for lower level nodes
  • Assuming BFS order guarantees bottom view
  • Ignoring horizontal distance updates
5. Given a binary tree, how would you modify the bottom view algorithm to handle nodes with the same horizontal distance and depth, ensuring the rightmost node is chosen in the bottom view?
hard
A. Use a queue for BFS and update map only if node depth is greater, replacing existing node
B. Use DFS and store only the first node encountered at each horizontal distance
C. Use a stack and update map only if node depth is less than current stored depth
D. Use BFS and update map only if node depth is greater or equal, replacing existing node

Solution

  1. Step 1: Understand the problem with same HD and depth

    When nodes share horizontal distance and depth, bottom view should show the rightmost node.
  2. Step 2: Modify BFS approach

    Use BFS to traverse level by level, and update map if current node's depth is greater or equal to stored node's depth, ensuring rightmost node replaces leftmost at same depth.
  3. Step 3: Implement depth tracking

    Track depth along with horizontal distance, update map accordingly during BFS.
  4. Final Answer:

    Use BFS and update map only if node depth is greater or equal, replacing existing node -> Option D
  5. Quick Check:

    Update map on equal or greater depth for rightmost node [OK]
Hint: Update map if depth >= current to keep rightmost node [OK]
Common Mistakes:
  • Ignoring depth when updating map
  • Using DFS which doesn't guarantee level order
  • Not handling equal depth nodes properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard