Reported January 2025
Mygatebinary tree

Left View of a Binary Tree

Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Mygate OA. Under 2s to a working solution.
Founder's read

Example 2 in the Mygate problem is the one that trips people: root 8 with only right children still returns [8,9,10]. Mygate reported this Left View of a Binary Tree OA in January 2025, and it's a clean tree traversal question. One value per depth, the leftmost node at each level. If you've seen level-order traversal, you've seen this. If your brain freezes on the clock, StealthCoder runs invisibly on the live OA and gives you a working solution as a safety net.

The problem

Given the root of a binary tree, return its left view as a list of node values from top to bottom.
At each depth, include the leftmost existing node when the nodes at that depth are ordered from left to right. Include one value for every nonempty depth, even when values repeat.
An empty tree returns an empty list. The tree is displayed in level order, using null for missing children.

Function
leftView(root: TreeNode) → List<Integer>

Examples
Example 1
root = [1,2,3,null,4,5,6]
return = [1,2,4]
The first nodes at depths 0, 1, and 2 have values 1, 2, and 4.
Example 2
root = [8,null,9,null,10]
return = [8,9,10]
There is one existing node at each depth. All three are visible despite having only right-child links.

Constraints
The tree has 0 to 5000 nodes.
Each node value is an integer in [-10^6, 10^6].
Each node has at most two children, and the input is a valid tree with no cycles or shared child nodes.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that left view means first node per depth, not following left child pointers. Two clean ways. BFS: process level by level, and record the first node dequeued at each level. DFS: visit the node, then left, then right, passing depth, and add the value only when depth equals the current result size. Both are O(n) time. The classic pitfall is walking only left children, which fails example 2 where every child is a right child. Another is handling the empty tree, which must return an empty list. With up to 5000 nodes, recursion depth could reach 5000 on a skewed tree, so BFS is the safer pick in some languages. Duplicate values don't matter, since you key on depth, not value. If you blank mid-assessment, StealthCoder is the hedge that reads the problem and hands you the level-order version.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Left View of a Binary Tree cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as binary tree right side view. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Mygate's OA.

Mygate reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Left View of a Binary Tree FAQ

What's the trick for Left View of a Binary Tree?+

Take the first node at each depth. Do a level-order traversal and record the first node of every level, or do a preorder DFS (node, left, right) and add a value when the depth equals the result list size. Don't just follow left pointers.

Why does example 2 return [8,9,10]?+

Each depth has exactly one node, so that node is the leftmost at its level. The nodes are all right children, but they're still visible from the left. This is why following only left children is wrong.

BFS or DFS for this problem?+

Either works in O(n). BFS is simpler to reason about and avoids deep recursion on a skewed tree of 5000 nodes. DFS with a depth check is shorter to write. Pick whichever you can code without bugs under pressure.

How hard is this one really?+

Easy to medium. It's a standard tree traversal variant. The only real risks are the right-child-only case and the empty tree. If you can write level-order traversal from memory, you can solve this in a few minutes.

How do I prepare for this in 48 hours?+

Write level-order BFS and preorder DFS with depth tracking from scratch, twice each. Then test on a skewed tree, an empty tree, and a tree where the left view comes from a right subtree. Those three cases cover most failures.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Mygate.

OA at Mygate?
Invisible during screen share
Get it