Bottom View of Binary Tree

Given a binary tree, determine and return its bottom view when viewed from left to right. Assume that each child of a node is positioned at a 45-degree angle from its parent.

Nodes in the bottom view are those that are visible when the tree is viewed from the bottom. For each horizontal distance from the root, the node that is visible in the bottom view is the one that is deepest (or the bottom-most node).

Example:

Input:
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output:
4 2 6 3 7
Explanation:

Given the tree:

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

The bottom-most node at various horizontal distances are:

  • -2: 4
  • -1: 2
  • 0: 6
  • 1: 3
  • 2: 7

Therefore, the bottom view is 4 2 6 3 7.

Constraints:

  • 1 ≤ T ≤ 100
  • 1 ≤ N ≤ 3000
  • -10^9 ≤ DATA ≤ 10^9
  • Time Limit: 1 sec
Note:
  • Horizontal distance of a node 'n' is calculated relative to the root. The root has a horizontal distance of 0. For any node, the horizontal distance is:
    - parent's distance + 1 if it is a right child.
    - parent's distance - 1 if it is a left child.
  • If two nodes share the same horizontal distance but are at different depths, select the deeper node.
  • No need for explicit print statements, just return the correct sequence of nodes from left to right in the bottom view.
AnswerBot
4mo

Given a binary tree, determine and return its bottom view when viewed from left to right.

  • Traverse the binary tree level by level and keep track of the horizontal distance and depth of each node

  • For eac...read more

Help your peers!
Select
Add answer anonymously...

Cognizant Software Analyst interview questions & answers

A Software Analyst was asked Q. Can you explain the projects you have worked on?
A Software Analyst was asked Q. Bottom View of Binary Tree Given a binary tree, determine and return its bottom ...read more
A Software Analyst was asked Q. DFS Traversal Problem Statement Given an undirected and disconnected graph G(V, ...read more

Popular interview questions of Software Analyst

A Software Analyst was asked Q1. Can you explain the projects you have worked on?
A Software Analyst was asked Q2. Bottom View of Binary Tree Given a binary tree, determine and return its bottom ...read more
A Software Analyst was asked Q3. DFS Traversal Problem Statement Given an undirected and disconnected graph G(V, ...read more
Cognizant Software Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits