Quick revision
The short answer
The left view of a binary tree is a level by level tree problem. The node visible from the left at each depth is the first node of that level, so a breadth first traversal with a queue, which reads the queue's size at the start of each level to know where the level ends, records the first node polled in every level. That is O(n) time and O(w) space, where w is the widest level; a depth first search that records the first node reached at each new depth works too.
The solution
What it costs
| Approach | Time | Extra space | Correct? |
|---|---|---|---|
| Follow left pointers from the root | O(h) | O(1) | no, misses 4 and 7 |
| Search each depth from the root | O(n h), O(n squared) skewed | O(h) | yes |
| BFS, one level at a time | O(n) | O(w) queue | yes |
| DFS, first node at each new depth | O(n) | O(h) call stack | yes |
What to remember
We found the nodes visible from the left of a binary tree, one per level, like the nearest lit room on each floor of a building seen from the side. Following left pointers misses nodes such as 4 and 7, and searching each depth from the root re-walks the upper levels for O(n h). A queue visits the levels once and in order; reading its size at the start of each level marks exactly where the level ends, and the first node polled is the one we see. That gives O(n) time and O(w) space, and a DFS that records the first node at each new depth is the O(h) space alternative. The clue to remember is anything per level or per depth in a tree: level order traversal with the queue size as the boundary.
1What is visible from the left, told as a building seen from the side
Imagine a tall building at night, with lights on in some rooms on each floor. You stand far away on its left side and look across. On every floor you see exactly one lit room, the one nearest to you, because it hides all the others on that floor. A floor whose lit rooms are all on the far right still shows you one of them, the leftmost of those. So from the side you see one room per floor, the leftmost lit room. Keep this building in mind, because a tree's left view works exactly the same way.
A binary tree is a set of nodes where each node has at most two children, a left one and a right one, and one node at the top, the root, has no parent. A node's depth, also called its level, is how many steps it is below the root, so the root is at level 0 and its children at level 1. Each level is a floor of our building. The left view is the list of the first node on each level, reading that level from left to right.
Let us use one tree throughout. The root is 1, with children 2 and 3. Node 2 has only a right child, 4. Node 3 has two children, 5 and 6, and node 6 has only a right child, 7. By hand, the levels are 1, then 2 3, then 4 5 6, then 7. Take the first node of each, and the left view is [1, 2, 4, 7].
Four levels. The marked nodes are the leftmost node of each level, the ones a person standing on the left would see.
- level 0
- 1
- level 1
- 2 3
- level 2
- 4 5 6
- level 3
- 7
- left view
- [1, 2, 4, 7]
1Level 0 is 1, level 1 is 2 3, level 2 is 4 5 6, and level 3 is 7. The first node of each level gives the left view, [1, 2, 4, 7].
What to assume
- Do we print the nodes, or return them?
- We return a list of values, which is easier to test.
- What does an empty tree give?
- An empty list.
- Is it a binary search tree?
- No, any binary tree, so the values tell us nothing about position.
- How deep can it be?
- Perhaps very deep if it leans to one side.
Look at nodes 4 and 7 in the picture. Both are right children, yet both are visible, because on their floors nothing sits further to the left. This is the detail that defeats the obvious approach, and the whole problem turns on it.
A careful candidate asks a few questions first. Do we print the nodes, or return them? We return a list of values, which is easier to test. What does an empty tree give? An empty list. Is it a binary search tree? No, any binary tree, so the values tell us nothing about position. How deep can it be? Perhaps very deep if it leans to one side, which matters if we use recursion.
Your turn
Solve it before you read on. Above is one example with the result expected for it. Under What to assume are the points this solution takes as given. Say your approach aloud or type it below, and write the code in your own editor. Then take one hint at a time and compare.
2How to recognise a level by level tree pattern
A beginner reading left view often thinks of left children, and starts at the root following left pointers. That is a fair first guess from the words, but views are about where nodes sit, not about which pointer led to them. We will see it fail in a moment.
The first clue is the word visible, or the phrase seen from the side. Seen from the side, a tree shows one node per level. So the answer has one entry for every depth, and we must think about the tree one level at a time.
The second clue is the word left. Within a level, we want the node that comes first when the level is read from left to right. So we need a way to visit each level's nodes in left to right order.
Together these clues point to the trees family and to a technique called level order traversal, also called breadth first search, or BFS. Traversal means visiting every node once. Breadth first means we finish a whole level, the full breadth of the tree, before going one level deeper, using a queue. The rule to carry to new problems is that any question asking for something per level, the left or right view, the average or the largest value of each level, or a zigzag order, points to BFS with the queue's size read at the start of each level.
The alternative is depth first search, or DFS, which follows one branch all the way down before trying the next. It can solve this problem too, if it carries the depth of each node along, and we will see how later. Following left pointers alone, though, is not a traversal at all, because it visits only one path.
3The honest first attempts, left pointers and then one search per depth
The first attempt follows the words, starting at the root and going to the left child again and again. From 1 we go to 2, and 2 has no left child, so we stop with [1, 2]. We missed 4 and 7, which are on the left of their levels even though they are right children. A left pointer is not the same as being leftmost in a level, so this attempt is simply wrong.
So let us try an honest brute force that is correct. We treat each depth as its own small question. First we compute the height of the tree, the number of levels, which is 4 here. Then for each depth d from 0 to 3, we search from the root for the first node at depth d, trying left before right everywhere. Because the search always tries the left side first, the first node it reaches at depth d is the leftmost one.
Each frame is one call of firstAtDepth. Nodes it visits are marked as compared, and the node it returns is marked as found.
- depth d
- 0
- visited now
- 1
- found
- 1
- visits so far
- 1
1
firstAtDepth(root, 0)visits only the root and returns 1. One node visited so far.- depth d
- 1
- visited now
- 1 2
- found
- 2
- visits so far
- 3
2For depth 1 the search starts again at the root, goes left to 2 and returns it. The root is visited a second time.
- depth d
- 2
- visited now
- 1 2 4
- found
- 4
- visits so far
- 6
3For depth 2 it starts at the root once more, goes to 2, finds no left child, tries the right, and returns 4. Three visits for one answer.
- depth d
- 3
- visited now
- 1 2 4 3 5 6 7
- found
- 7
- visits so far
- 13
4For depth 3 the left side, 2 and 4, has nothing that deep, so the search crosses to 3, 5 and 6 before finding 7. Seven visits, and 13 in all.
It works, and it gives [1, 2, 4, 7]. Now let us count the work on the example. The four searches visited 1, 2, 3 and 7 nodes, 13 visits for a tree of 7 nodes. The root alone was visited four times, and node 2 three times.
For a tree of n nodes and h levels, each search can visit most of the nodes above its depth, so the total is about n times h, written O(n h). For a balanced tree h is small, but for a tree that leans entirely to one side h is n, and the cost becomes O(n squared). The waste is plain in the picture: every search starts again at the root and walks back down through levels that earlier searches had already explored. So the question to ask is whether we could visit the levels once, in order, and note the first node of each as we pass it.
4Getting to one level at a time with a queue
The first observation is that visiting levels in order is exactly what a queue gives us. A queue works like a line at a ticket counter: we offer a node to join at the back and poll the node at the front. We start with the root in the queue. Then, as long as the queue is not empty, we poll the front node and offer its left child and then its right child at the back. Children always join behind everyone already waiting, so all nodes of one level leave before any node of the next, and within a level they leave from left to right.
The second observation tells us where a level ends. At the moment a level begins, the queue holds exactly that level's nodes and nothing else, because the level before it has just been fully polled. So we read levelSize = queue.size() once, before the inner loop, and poll exactly that many nodes. Any children offered during that loop belong to the next level and wait their turn. The first node polled in each round, when i is 0, is the leftmost node of its level, so that is the one we record.
A beginner often asks why we copy the size into levelSize at all, instead of writing i < queue.size() in the loop. Let us try it and see. At level 0 we poll 1 and offer 2 and 3, so the size grows to 2, and the loop keeps going and polls 2 as if it were on level 0. The levels blur together, and on our tree the program returns [1, 3, 5, 6, 7], which is wrong. The queue's size changes while we work, so we must read it once, before the level starts.
Why is the answer always right? Within each round, the queue hands out one whole level in left to right order, because each level's nodes were offered in the order their parents were polled, and each parent offered its left child before its right. So the node polled with i equal to 0 is always the leftmost node of that level. That is how node 4 comes first on level 2: its parent 2 was polled before 3, so 4 joined the queue before 5 and 6.
The DFS alternative uses a different idea. It visits the left child before the right and passes each node's depth along. The first time the search reaches a new depth, it must be standing on that depth's leftmost node, because everything to its left has already been searched. So it records a node exactly when depth equals the number of nodes recorded so far. It uses the call stack instead of a queue, which is fine for balanced trees but risky for very deep ones.
5Watching the queue carry the tree level by level
The picture plays leftView on our tree. Nodes waiting in the queue are marked as compared, polled nodes that were hidden are done, and nodes recorded in the view are found. The panel shows the level, levelSize, the loop counter i, the queue from front to back, and the view so far.
Watch the moment each level begins, because the queue then holds exactly one level and levelSize is read from it. Watch also where node 4 sits in the queue.
Done: polled and hidden. Compared: waiting in the queue. Found: recorded in the view. Now: the node being polled.
- level
- 0
- levelSize
- 1
- i
- -
- queue
- 1
- view
- []
1We put the root in the queue. At the start of level 0 the queue holds 1, so
levelSizeis 1.- level
- 0
- levelSize
- 1
- i
- 0
- queue
- 2 3
- view
- [1]
2i = 0, so 1 is the first node of its level and goes into the view. Its children 2 and 3 join the back of the queue.
- level
- 1
- levelSize
- 2
- i
- -
- queue
- 2 3
- view
- [1]
3Level 1 begins. The queue holds exactly this level, 2 then 3, so
levelSizeis 2 and we will poll two nodes.- level
- 1
- levelSize
- 2
- i
- 0
- queue
- 3 4
- view
- [1, 2]
4We poll 2 with i = 0, so 2 goes into the view. Its only child, 4, joins the back, behind 3.
- level
- 1
- levelSize
- 2
- i
- 1
- queue
- 4 5 6
- view
- [1, 2]
5We poll 3 with i = 1, so it is hidden behind 2. Its children 5 and 6 join the back, after 4.
- level
- 2
- levelSize
- 3
- i
- -
- queue
- 4 5 6
- view
- [1, 2]
6Level 2 begins with 4, 5, 6 in the queue, so
levelSizeis 3. Node 4 is first, even though it is a right child.- level
- 2
- levelSize
- 3
- i
- 0
- queue
- 5 6
- view
- [1, 2, 4]
7We poll 4 with i = 0, and it goes into the view. It has no children, so nothing joins the queue.
- level
- 2
- levelSize
- 3
- i
- 1
- queue
- 6
- view
- [1, 2, 4]
8We poll 5 with i = 1. It is hidden behind 4 and has no children.
- level
- 2
- levelSize
- 3
- i
- 2
- queue
- 7
- view
- [1, 2, 4]
9We poll 6 with i = 2, the last of this level. Its right child 7 joins the queue for the next level.
- level
- 3
- levelSize
- 1
- i
- -
- queue
- 7
- view
- [1, 2, 4]
10Level 3 begins. The queue holds only 7, so
levelSizeis 1.- level
- 3
- levelSize
- 1
- i
- 0
- queue
- empty
- view
- [1, 2, 4, 7]
11We poll 7 with i = 0, so 7 goes into the view. It has no children, and the queue is now empty.
- queue
- empty
- view
- [1, 2, 4, 7]
12The queue is empty, so the outer loop stops and we return the view, [1, 2, 4, 7], one node from each of the four levels.
Let us read the important frames back. When level 1 begins, the queue holds 2 and 3, so levelSize is 2. Polling 2 offers 4, and 4 waits behind 3, which is why the inner loop stops after two polls and does not take 4 too early.
When level 2 begins, the queue holds 4, 5 and 6, in that order, because 2 was polled before 3. So 4 is polled first and recorded, although it is a right child, and 5 and 6 are hidden behind it. Level 3 holds only 7, so 7 is recorded, and the view is [1, 2, 4, 7].
6The complete Java program
Here is the whole program. The Node is a record, a short way in Java to write a class that only holds values, here a value and two children. The class has the level order method and the DFS alternative, and main runs both on our tree and the level order method on an empty tree.
Let us read leftView in the order it runs. We create the result list and return it at once if the tree is empty. Then we create an ArrayDeque, Java's recommended queue, and offer the root.
The outer while loop runs once per level, as long as some node is waiting. Its first line reads levelSize from the queue, exactly the number of nodes on this level, before anything is added. The inner for loop then polls exactly levelSize nodes. When i is 0, the node is the leftmost of its level, so its value goes into view. Each polled node offers its left child and then its right child, skipping missing ones, and these wait at the back for the next round.
leftViewDfs starts with depth 0 and an empty list. If a node's depth equals view.size(), no node at that depth has been recorded yet, so this one, the first reached, is added. Then it searches left before right with depth + 1. main prints [1, 2, 4, 7] twice and then []. For the right view, the only change is recording the node when i equals levelSize - 1.
7How much it costs, and the cases at the edges
Let us count on the example first. Each of the 7 nodes was offered once and polled once, so each was handled once, while the brute force made 13 visits. The gap grows quickly with depth: on a chain of 1,000 nodes the brute force makes about half a million visits. For n nodes, every node enters and leaves the queue once with constant work, so the time is O(n).
The queue holds at most one level plus part of the next, so the extra space is O(w), where w is the number of nodes on the widest level. In a full tree the bottom level holds about half the nodes, so w can be about n / 2. The DFS version uses O(h) space for the call stack instead, where h is the height.
| Approach | Time | Extra space | Correct? |
|---|---|---|---|
| Follow left pointers from the root | O(h) | O(1) | no, misses 4 and 7 |
| Search each depth from the root | O(n h), O(n squared) skewed | O(h) | yes |
| BFS, one level at a time | O(n) | O(w) queue | yes |
| DFS, first node at each new depth | O(n) | O(h) call stack | yes |
The edge cases are short. An empty tree returns an empty list before the queue is created. A single node returns [root]. A tree that leans entirely to the right, a chain of right children, shows every node, because each level has exactly one node; that is precisely the case that following left pointers gets wrong.
For very deep, thin trees we prefer the BFS, because the recursive DFS makes one waiting call per level and could overflow Java's call stack. For very wide, shallow trees the DFS uses less memory, because its stack only grows with the height.
8Follow-up questions
How do you get the right view instead?
In the BFS we record the last node polled in each level, when i equals levelSize - 1, instead of the first. On our tree that gives 1, 3, 6 and 7. In the DFS we visit the right child before the left, so the first node reached at each new depth is the rightmost. Both keep the same O(n) cost.
How is the left view different from the left boundary of a tree?
The left boundary, used in the boundary traversal question, is a path. From the root it follows the left child, or the right child when there is no left child, down towards a leaf. On our tree that path is 1, 2, 4, and it stops at the leaf 4. It matches the first three entries of the left view, but it can never include 7, which hangs under a different branch. A view picks one node per level wherever it sits; a boundary follows one path.
Can you do it without knowing the queue size, for example with a marker?
Yes. An older technique offers a special marker, often null, after each level. Whenever we poll the marker, a level has ended, so the next node polled is the first of a new level, and if the queue is not empty we offer the marker again. It works, but ArrayDeque does not accept null, and the size snapshot is shorter and harder to get wrong.
9Conclusion
We found the nodes visible from the left of a binary tree, one per level, like the nearest lit room on each floor of a building seen from the side. Following left pointers misses nodes such as 4 and 7, and searching each depth from the root re-walks the upper levels for O(n h). A queue visits the levels once and in order; reading its size at the start of each level marks exactly where the level ends, and the first node polled is the one we see. That gives O(n) time and O(w) space, and a DFS that records the first node at each new depth is the O(h) space alternative. The clue to remember is anything per level or per depth in a tree: level order traversal with the queue size as the boundary.
Open with
Let me check the definition: from the left I see one node per depth, the leftmost on that level, and I return their values, with an empty list for an empty tree.
Then, if they want more
Following left children does not work, because a right child can be the leftmost node on its level, like 4 and 7 here. A brute force computes the height and, for each depth, searches from the root trying left before right, returning the first node at that depth.
That re-walks the upper levels every time, so it is O(n times h), quadratic for a skewed tree. Since this is a per level question, I use level order traversal with a queue. I offer the root, and while the queue is not empty I read its size, which is exactly the number of nodes on the current level.
I poll that many nodes, record the first one, and offer each node's children, which form the next level. Every node is queued once, so it is O(n) time and O(w) space, where w is the widest level.
A depth first alternative visits left before right and records a node when its depth equals the size of the result, using O(h) space. For the right view, I record the last node of each level instead.
About this answer
- Last updated
- 15 November 2026
- Written for
- Java 21, using only java.util
- Checked
- The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
- Where the question comes from
- Public interview write-ups, reworded by Scrivio; counted 47 times in that collection. How it is counted
- Official documentation
- ArrayDeque (Java 21), ArrayList (Java 21), java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Print the nodes visible from the left side of a binary tree.": https://scrivio-interview-library.netlify.app/data-structures/solutions/print-the-nodes-visible-from-the-left-side-of-a-binary-tree-57bdc1/