Left View of Binary Tree

Easy Trees & BST Views Original on GeeksforGeeks

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, 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 0 to 10^5 nodes.

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 Java code.

ApproachTimeSpace
BFS, first node per levelO(n)O(w)
DFS, left child firstO(n)O(h)

1BFS, first node per level

TimeO(n)
SpaceO(w)

In level order, record the first node polled on each level.

  1. For each level: record the node at i == 0.
Java
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;
    }
}

2DFS, left child first

TimeO(n)
SpaceO(h)

Preorder DFS visiting left before right. The first time you reach a new depth, that node is the leftmost on its level.

  1. If depth == out.size(), add node.val.
  2. Recurse left, then right, with depth + 1.
Java
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);
    }
}

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.