Left View of a Binary Tree - Visualization

Visualization Player

Problem Statement

Given a binary tree, return the left view of the tree. The left view of a binary tree contains the nodes that are visible when the tree is viewed from the left side. You must consider the leftmost node at each level of the tree and collect them in a top-down manner.

Examples

Input Tree Left View Output Description
[1, 2, 3, 4, 5, null, 6]
[1, 2, 4] Standard binary tree with left and right children; leftmost node at each level forms the left view
[1]
[1] Single node tree; the root is the only node and hence the left view
[] [] Empty tree; no nodes result in an empty left view
[1, 2, null, 3, null, null, null, 4]
[1, 2, 3, 4] Left-skewed tree; each level has only one leftmost node
[1, null, 2, null, null, null, 3]
[1, 2, 3] Right-skewed tree; the first node at each level still defines the left view
[10, 5, 15, 3, 7, null, 18]
[10, 5, 3] Binary search tree; leftmost visible node from each level contributes to the left view

Solution

Understanding the Problem

The Left View of a Binary Tree refers to the set of nodes visible when the tree is viewed from the left side. At each level of the tree, the leftmost node is considered part of the left view.

To extract this view, we need to explore the tree level by level (using Breadth First Search). At each level, the first node we encounter from left to right is the one visible from the left side.

Step-by-Step Solution with Example

step 1: Visualize the tree

Let's take a sample binary tree:


       1
     /       2     3
   /        4   5     6

When viewed from the left side, we see: 1 → 2 → 4

step 2: Use Level Order Traversal

We perform a BFS traversal using a queue. At each level, we process all nodes, and the first node we encounter is added to the left view list.

step 3: Initialize the queue

Start with the root node in the queue. We'll also track the level size to identify the first node at each level.

step 4: Traverse each level

For each level:

  • Record the first node we dequeue (this is the leftmost for the level).
  • Enqueue its left and right children, if they exist.

step 5: Build the result

Collect the first node from each level and add it to the result list representing the left view.

step 6: Output the final view

For the example above, the result would be: [1, 2, 4]

Edge Cases

Case 1: Left-Skewed Tree

If the tree only has left children like [10, 20, 30], all nodes are visible from the left. Output: [10, 20, 30]

Case 2: Right-Skewed Tree

Even when all nodes are on the right side like [7, null, 8, null, 9], each level still has only one node, so all nodes are visible. Output: [7, 8, 9]

Case 3: Tree with Missing Left Children

In some levels, the left child might be missing but the right child exists. The algorithm should still pick the first node seen at that level, regardless of being a left or right child.

Case 4: Empty Tree

If the tree is empty (null root), the output should be an empty list: []

Finally

To summarize, solving the Left View of a Binary Tree involves understanding that we need the first node seen at every level during a BFS traversal. By using a queue and processing level by level, we can handle any shape of tree—including skewed, sparse, or empty trees. This approach is efficient and beginner-friendly.

Algorithm Steps

  1. If the binary tree is empty, return an empty list.
  2. Initialize a queue and add the root node.
  3. While the queue is not empty, determine the number of nodes at the current level.
  4. For each node at the current level, dequeue the node. If it is the first node in this level, record its value.
  5. Enqueue the left and right children of the node, if they exist.
  6. Repeat until all levels are processed. The recorded values form the left view of the tree.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
TS
#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
    int val;
    struct TreeNode *left, *right;
};

struct TreeNode* createNode(int val) {
    struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    node->val = val;
    node->left = node->right = NULL;
    return node;
}

int* leftView(struct TreeNode* root, int* returnSize) {
    if (!root) {
        *returnSize = 0;
        return NULL;
    }
    struct TreeNode** queue = malloc(100 * sizeof(struct TreeNode*));
    int* result = malloc(100 * sizeof(int));
    int front = 0, rear = 0, resIndex = 0;
    queue[rear++] = root;
    while (front < rear) {
        int levelSize = rear - front;
        for (int i = 0; i < levelSize; i++) {
            struct TreeNode* node = queue[front++];
            if (i == 0) result[resIndex++] = node->val;
            if (node->left) queue[rear++] = node->left;
            if (node->right) queue[rear++] = node->right;
        }
    }
    *returnSize = resIndex;
    free(queue);
    return result;
}

int main() {
    struct TreeNode* root = createNode(1);
    root->left = createNode(2);
    root->right = createNode(3);
    root->left->left = createNode(4);
    root->left->right = createNode(5);
    root->right->right = createNode(6);
    int returnSize;
    int* res = leftView(root, &returnSize);
    printf("Left View: ");
    for (int i = 0; i < returnSize; i++) {
        printf("%d ", res[i]);
    }
    printf("\n");
    free(res);
    return 0;
}

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...