Left View of a Binary Tree

Given a binary tree, your task is to print the left view of the tree. The left view of a binary tree contains the nodes visible when the tree is viewed from the left side.

Input:

The input consists of elements in a level order form. Values of nodes are separated by a single space in a single line. Use -1 to represent null nodes.

Output:

The output will be a single line with the data values of the nodes visible in the left view of the tree, separated by a single space.

Example:

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

Explanation:

The input represents a binary tree in level order. After constructing the tree, the left view contains the nodes 1, 2, 4, and 7, as these are the nodes visible from the left side.

Constraints:

  • 0 <= N <= 10^5
  • 1 <= data <= 10^5
  • The sequence represents a binary tree where 'N' is the total number of nodes and 'data' is the value of each node.
Note:
The input format explains how to interpret and construct the binary tree from the level order input. All nodes at the last level being null (-1) indicates the end of input.
AnswerBot
4mo

The task is to print the left view of a binary tree, which contains the nodes visible when viewed from the left side.

  • Traverse the binary tree level by level and keep track of the leftmost node at each...read more

Help your peers!
Select
Add answer anonymously...

SquadStack Product Engineer interview questions & answers

A Product Engineer was asked Q. Reverse Words in a String: Problem Statement You are given a string of length N....read more
A Product Engineer was asked Q. What is an Inner Join?
A Product Engineer was asked Q. How would you design a parking lot system?

Popular interview questions of Product Engineer

A Product Engineer was asked Q1. Reverse Words in a String: Problem Statement You are given a string of length N....read more
A Product Engineer was asked Q2. What is an Inner Join?
A Product Engineer was asked Q3. How would you design a parking lot system?
SquadStack Product Engineer 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