Prepin
Log in

M

O

Display the left view of a binary tree.

Approach / Explanation

This question requires you to develop a program that displays the left view of a binary tree. A binary tree is a data structure where each node can have no more than two children, commonly known as the left child and the right child. The "left view" of a binary tree consists of the nodes that are visible when observing the tree from the left side. Specifically, if you envision a line drawn from the far left of the binary tree toward the root and then extending to the far right, the nodes that this line intersects represent the "left view". To tackle this question, you can utilize a breadth-first search to navigate through the tree level by level from left to right. At each level, you would print the first node you encounter. Alternatively, you could perform a depth-first search by exploring the tree from left to right while keeping track of the current level and the highest level reached so far. You would print the node if the current level exceeds the maximum level recorded.

Suggested Answer

This is a Python example that demonstrates a breadth-first search using a queue: def leftView ( root ) : if ( not root ) : return q = [ ] q . append ( root ) while ( len ( q ) ) : n = len ( q ) for i in range ( 1 , n + 1 ) : temp = q [ 0 ] q . pop ( 0 ) if ( i == 1 ) : print ( temp . data , end = " " ) if ( temp . left != None ) : q . append ( temp . left ) if ( temp . right != None ) : q . append ( temp . right ) In the provided code, we first check if the tree's root is null. If it is, we return immediately since there is no tree to display. If the root is not null, we enqueue the root node (q). We then continue to traverse the queue until it becomes empty, printing the first element of each level and appending the left and right children of each node to the end of the queue.

Alternative Answer

Here’s a Python illustration that employs a recursive depth-first search: maxLevel = [ 0 ] def leftViewUtil ( root , level , maxLevel ) : if root is None : return if ( maxLevel [ 0 ] < level ) : print ( f" { root . data } " , end = " " ) maxLevel [ 0 ] = level leftViewUtil ( root . left , level + 1 , maxLevel ) leftViewUtil ( root . right , level + 1 , maxLevel ) def leftView ( root ) : leftViewUtil ( root , 1 , maxLevel ) This code defines a helper function named leftViewUtil , which performs a depth-first traversal of the binary tree. It takes the current node, the current level in the tree, and the maximum level reached so far (stored in a list for mutability) as parameters. The function first checks if the node is null. If it is, the function returns. If the node is not null and the current level exceeds the maximum level reached, it prints the node and updates the maximum level. Subsequently, it makes recursive calls for the left and right children of the current node. The leftView function initiates the process by calling leftViewUtil with the tree's root, starting at level 1, and the maxLevel variable.

Question Details

Difficulty & Category

Medium

Companies

Amazon
Meta (Facebook)
Oracle

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.