Left View of Binary Tree
Left View of Binary Tree is a easy trees & bst problem solved with the views pattern.
The best approach, dfs, left child first, runs in O(n) time and O(h) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from bfs, first node per level up.
Problem
Return the left view of a binary tree: the first node visible on each level when looking from the left side, from top to bottom.
Examples
Example 1
- Input
root = [1, 2, 3, null, 5, null, 4]- Output
[1, 2, 5]
Example 2
- Input
root = [1, null, 3]- Output
[1, 3]- Why
- The left view can include right children when nothing is to their left.
Constraints
- The tree has
0to10^5nodes.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| BFS, first node per level | O(n) | O(w) |
| DFS, left child first | O(n) | O(h) |
1BFS, first node per level
O(n)O(w)In level order, record the first node polled on each level.
- For each level: record the node at i == 0.
class Solution {
ArrayList<Integer> leftView(TreeNode root) {
ArrayList<Integer> out = new ArrayList<>();
if (root == null) return out;
Queue<TreeNode> q = new ArrayDeque<>();
q.add(root);
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode n = q.poll();
if (i == 0) out.add(n.val);
if (n.left != null) q.add(n.left);
if (n.right != null) q.add(n.right);
}
}
return out;
}
}class Solution:
def leftView(self, root: Optional[TreeNode]) -> List[int]:
out = []
if root is None:
return out
q = deque([root])
while q:
size = len(q)
for i in range(size):
n = q.popleft()
if i == 0:
out.append(n.val)
if n.left is not None:
q.append(n.left)
if n.right is not None:
q.append(n.right)
return outclass Solution {
public:
vector<int> leftView(TreeNode* root) {
vector<int> out;
if (root == nullptr) return out;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode* n = q.front(); q.pop();
if (i == 0) out.push_back(n->val);
if (n->left != nullptr) q.push(n->left);
if (n->right != nullptr) q.push(n->right);
}
}
return out;
}
};function leftView(root) {
const out = [];
if (root === null) return out;
let q = [root];
while (q.length > 0) {
const size = q.length;
const next = [];
for (let i = 0; i < size; i++) {
const n = q[i];
if (i === 0) out.push(n.val);
if (n.left !== null) next.push(n.left);
if (n.right !== null) next.push(n.right);
}
q = next;
}
return out;
}2DFS, left child first
O(n)O(h)Preorder DFS visiting left before right. The first time you reach a new depth, that node is the leftmost on its level.
- If depth == out.size(), add node.val.
- Recurse left, then right, with depth + 1.
class Solution {
ArrayList<Integer> leftView(TreeNode root) {
ArrayList<Integer> out = new ArrayList<>();
dfs(root, 0, out);
return out;
}
private void dfs(TreeNode n, int depth, ArrayList<Integer> out) {
if (n == null) return;
if (depth == out.size()) out.add(n.val);
dfs(n.left, depth + 1, out);
dfs(n.right, depth + 1, out);
}
}class Solution:
def leftView(self, root: Optional[TreeNode]) -> List[int]:
out = []
self.dfs(root, 0, out)
return out
def dfs(self, n: Optional[TreeNode], depth: int, out: List[int]) -> None:
if n is None:
return
if depth == len(out):
out.append(n.val)
self.dfs(n.left, depth + 1, out)
self.dfs(n.right, depth + 1, out)class Solution {
public:
vector<int> leftView(TreeNode* root) {
vector<int> out;
dfs(root, 0, out);
return out;
}
private:
void dfs(TreeNode* n, int depth, vector<int>& out) {
if (n == nullptr) return;
if (depth == (int)out.size()) out.push_back(n->val);
dfs(n->left, depth + 1, out);
dfs(n->right, depth + 1, out);
}
};function leftView(root) {
const out = [];
dfs(root, 0, out);
return out;
}
function dfs(n, depth, out) {
if (n === null) return;
if (depth === out.length) out.push(n.val);
dfs(n.left, depth + 1, out);
dfs(n.right, depth + 1, out);
}Edge cases to test
- Empty tree
- Only right children
Hints
Hint 1
The left view is the first node of each level.
FAQ
What is the best time complexity for Left View of Binary Tree?
DFS, left child first runs in O(n) time and O(h) extra space.
Which pattern does Left View of Binary Tree use?
It is a trees & bst problem that uses the views pattern. Other problems with the same pattern: Top View of Binary Tree, Bottom View of Binary Tree, Binary Tree Right Side View.
Is there a brute force solution for Left View of Binary Tree?
Yes. BFS, first node per level takes O(n) time and O(w) space. In level order, record the first node polled on each level.
Which edge cases should I test for Left View of Binary Tree?
Empty tree; Only right children.