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.
Be the first one to answer
Add answer anonymously...
SquadStack Product Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter