Left View of a Binary Tree

Last Updated : 3 Aug, 2026

Given the root of a binary tree. Return the left view of the binary tree. The left view of a binary tree is the set of nodes visible when the tree is viewed from the left side.

Note: If the tree is empty, return an empty list.

Examples:

Input: root = [1, 2, 3, 4, 5, N, N]

1

Output: [1, 2, 4]
Explanation: From the left side of the tree, only the nodes 1, 2, and 4 are visible.

2

Input: root = [1, 2, 3, N, N, 4, N, N, 5, N, N]

3

Output: [1, 2, 4, 5]
Explanation: From the left side of the tree, only the nodes 1, 2, 4, and 5 are visible.

4
Try It Yourself
redirect icon

Level Order Traversal (BFS) - O(n) Time and O(n) Space

The idea is to traverse the tree level by level using a queue. At each level, the first node processed is the leftmost node, so we add it to the answer.

Working of Approach:

  • If the tree is empty, return an empty vector.
  • Perform level order traversal using a queue.
  • Process one level at a time.
  • Store the first node of every level in the result.
  • Continue until all levels are traversed.
C++
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>
using namespace std;

class Node
{
  public:
    int data;
    Node *left;
    Node *right;

    Node(int val)
    {
        data = val;
        left = right = nullptr;
    }
};

vector<int> leftView(Node *root)
{

    vector<int> res;

    // If the tree is empty
    if (root == nullptr)
        return res;

    queue<Node *> q;
    q.push(root);

    // Perform level order traversal
    while (!q.empty())
    {

        int levelSize = q.size();

        for (int i = 0; i < levelSize; i++)
        {

            Node *curr = q.front();
            q.pop();

            // First node of current level is part of left view
            if (i == 0)
                res.push_back(curr->data);

            // Push left child
            if (curr->left)
                q.push(curr->left);

            // Push right child
            if (curr->right)
                q.push(curr->right);
        }
    }

    return res;
}

int main()
{

    // Tree:
    //          1
    //        /   \
    //       2     3
    //            /
    //           4
    //            \
    //             5

    Node *root = new Node(1);

    root->left = new Node(2);
    root->right = new Node(3);

    root->right->left = new Node(4);

    root->right->left->right = new Node(5);

    vector<int> ans = leftView(root);

    cout << "[";

    for (int i = 0; i < ans.size(); i++)
    {
        cout << ans[i];
        if (i != ans.size() - 1)
            cout << ", ";
    }

    cout << "]";

    return 0;
}
Java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

class Node {
    public int data;
    public Node left;
    public Node right;

    Node(int val)
    {
        data = val;
        left = right = null;
    }
}

public class GFG {
    public static ArrayList<Integer> leftView(Node root)
    {
        ArrayList<Integer> res = new ArrayList<>();

        // If the tree is empty
        if (root == null)
            return res;

        Queue<Node> q = new LinkedList<>();
        q.add(root);

        // Perform level order traversal
        while (!q.isEmpty()) {
            int levelSize = q.size();

            for (int i = 0; i < levelSize; i++) {
                Node curr = q.poll();

                // First node of current level is part of
                // left view
                if (i == 0)
                    res.add(curr.data);

                // Push left child
                if (curr.left != null)
                    q.add(curr.left);

                // Push right child
                if (curr.right != null)
                    q.add(curr.right);
            }
        }

        return res;
    }

    public static void main(String[] args)
    {

        // Tree:
        //          1
        //        /   \
        //       2     3
        //             /
        //           4
        //            \
        //             5

        Node root = new Node(1);

        root.left = new Node(2);
        root.right = new Node(3);

        root.right.left = new Node(4);

        root.right.left.right = new Node(5);

        ArrayList<Integer> ans = leftView(root);

        System.out.print("[");

        for (int i = 0; i < ans.size(); i++) {
            System.out.print(ans.get(i));
            if (i != ans.size() - 1)
                System.out.print(", ");
        }

        System.out.print("]");
    }
}
Python
from collections import deque


class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None


def leftView(root):
    res = []

    # If the tree is empty
    if root is None:
        return res

    q = deque([root])

    # Perform level order traversal
    while q:
        levelSize = len(q)

        for i in range(levelSize):
            curr = q.popleft()

            # First node of current level is part of left view
            if i == 0:
                res.append(curr.data)

            # Push left child
            if curr.left:
                q.append(curr.left)

            # Push right child
            if curr.right:
                q.append(curr.right)

    return res


if __name__ == '__main__':

    # Tree:
    #          1
    #        /   \
    #       2     3
    #             /
    #           4
    #            \
    #             5

    root = Node(1)

    root.left = Node(2)
    root.right = Node(3)

    root.right.left = Node(4)

    root.right.left.right = Node(5)

    ans = leftView(root)

    print('[', end='')

    for i in range(len(ans)):
        print(ans[i], end='')
        if i != len(ans) - 1:
            print(', ', end='')

    print(']')
C#
using System;
using System.Collections.Generic;

public class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int val)
    {
        data = val;
        left = right = null;
    }
}

public class GFG {
    public static List<int> leftView(Node root)
    {
        List<int> res = new List<int>();

        // If the tree is empty
        if (root == null)
            return res;

        Queue<Node> q = new Queue<Node>();
        q.Enqueue(root);

        // Perform level order traversal
        while (q.Count > 0) {
            int levelSize = q.Count;

            for (int i = 0; i < levelSize; i++) {
                Node curr = q.Dequeue();

                // First node of current level is part of
                // left view
                if (i == 0)
                    res.Add(curr.data);

                // Push left child
                if (curr.left != null)
                    q.Enqueue(curr.left);

                // Push right child
                if (curr.right != null)
                    q.Enqueue(curr.right);
            }
        }

        return res;
    }

    public static void Main()
    {

        // Tree:
        //          1
        //        /   \
        //       2     3
        //             /
        //           4
        //            \
        //             5

        Node root = new Node(1);

        root.left = new Node(2);
        root.right = new Node(3);

        root.right.left = new Node(4);

        root.right.left.right = new Node(5);

        List<int> ans = leftView(root);

        Console.Write('[');

        for (int i = 0; i < ans.Count; i++) {
            Console.Write(ans[i]);
            if (i != ans.Count - 1)
                Console.Write(", ");
        }

        Console.Write(']');
    }
}
JavaScript
class Node {
    constructor(val)
    {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

function leftView(root)
{
    let res = [];

    // If the tree is empty
    if (root === null)
        return res;

    let q = [];
    q.push(root);

    // Perform level order traversal
    while (q.length > 0) {
        let levelSize = q.length;

        for (let i = 0; i < levelSize; i++) {
            let curr = q.shift();

            // First node of current level is part of left
            // view
            if (i === 0)
                res.push(curr.data);

            // Push left child
            if (curr.left !== null)
                q.push(curr.left);

            // Push right child
            if (curr.right !== null)
                q.push(curr.right);
        }
    }

    return res;
}

// Driver Code
// Tree:
//          1
//        /   \
//       2     3
//             /
//           4
//            \
//             5

let root = new Node(1);

root.left = new Node(2);
root.right = new Node(3);

root.right.left = new Node(4);

root.right.left.right = new Node(5);

let ans = leftView(root);

console.log("[");

for (let i = 0; i < ans.length; i++) {
    process.stdout.write(ans[i].toString());
    if (i !== ans.length - 1)
        process.stdout.write(", ");
}

console.log("]");

Output
[1, 2, 4, 5]

DFS (Preorder Traversal) - O(n) Time and O(h) Space

The idea is to perform a preorder DFS traversal while keeping track of the current level. Since the left subtree is visited before the right subtree, the first node visited at every level forms the left view.

Working of Approach:

  • Start DFS from the root with level 0.
  • If a level is visited for the first time, store the current node.
  • Traverse the left subtree before the right subtree.
  • The first node reached at every level becomes part of the left view.
  • Continue recursively until all nodes are visited.

Let us understand with an example:
Input: root = [1, 2, 3, N, N, 4, N, N, 5, N, N]

3
  • Start DFS from the root (1) at level 0. Since this is the first node at level 0, add 1 to the result.
  • Move to the left child (2) at level 1. It is the first node at this level, so add 2 to the result.
  • The left subtree of 2 is empty, so backtrack and visit the right subtree of the root (3). Since level 1 is already visited, do not add 3.
  • Traverse to node 4 at level 2. It is the first node at this level, so add 4 to the result. Then visit node 5 at level 3 and add it as the first node of that level.
  • The traversal ends, and the final left view is [1, 2, 4, 5].
4
C++
#include <iostream>
#include <queue>
#include <sstream>
#include <vector>
using namespace std;

class Node
{
  public:
    int data;
    Node *left;
    Node *right;

    Node(int val)
    {
        data = val;
        left = right = nullptr;
    }
};

void recLeftView(Node *root, int level, vector<int> &res)
{
    if (root == nullptr)
        return;

    // first node of current level
    if (level == res.size())
    {
        res.push_back(root->data);
    }

    recLeftView(root->left, level + 1, res);
    recLeftView(root->right, level + 1, res);
}

vector<int> leftView(Node *root)
{
    vector<int> res;
    recLeftView(root, 0, res);
    return res;
}

int main()
{

    // Hardcoded tree:
    //          1
    //        /   \
    //       2     3
    //            /
    //           4
    //            \
    //             5

    Node *root = new Node(1);

    root->left = new Node(2);
    root->right = new Node(3);

    root->right->left = new Node(4);

    root->right->left->right = new Node(5);

    vector<int> ans = leftView(root);

    cout << "[";

    for (int i = 0; i < ans.size(); i++)
    {
        cout << ans[i];
        if (i != ans.size() - 1)
            cout << ", ";
    }

    cout << "]";

    return 0;
}
Java
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int val)
    {
        data = val;
        left = right = null;
    }
}

public class GFG {
    public static void recLeftView(Node root, int level,
                                   ArrayList<Integer> res)
    {
        if (root == null)
            return;

        // first node of current level
        if (level == res.size()) {
            res.add(root.data);
        }

        recLeftView(root.left, level + 1, res);
        recLeftView(root.right, level + 1, res);
    }

    public static ArrayList<Integer> leftView(Node root)
    {
        ArrayList<Integer> res = new ArrayList<>();
        recLeftView(root, 0, res);
        return res;
    }

    public static void main(String[] args)
    {

        // Tree:
        //          1
        //        /   \
        //       2     3
        //             /
        //           4
        //            \
        //             5

        Node root = new Node(1);

        root.left = new Node(2);
        root.right = new Node(3);

        root.right.left = new Node(4);

        root.right.left.right = new Node(5);

        ArrayList<Integer> ans = leftView(root);

        System.out.print("[");

        for (int i = 0; i < ans.size(); i++) {
            System.out.print(ans.get(i));
            if (i != ans.size() - 1)
                System.out.print(", ");
        }

        System.out.print("]");
    }
}
Python
class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None


def recLeftView(root, level, res):
    if root is None:
        return

    # first node of current level
    if level == len(res):
        res.append(root.data)

    recLeftView(root.left, level + 1, res)
    recLeftView(root.right, level + 1, res)


def leftView(root):
    res = []
    recLeftView(root, 0, res)
    return res


if __name__ == '__main__':

    # Tree:
    #          1
    #        /   \
    #       2     3
    #             /
    #           4
    #            \
    #             5

    root = Node(1)

    root.left = Node(2)
    root.right = Node(3)

    root.right.left = Node(4)

    root.right.left.right = Node(5)

    ans = leftView(root)

    print('[', end='')

    for i in range(len(ans)):
        print(ans[i], end='')
        if i != len(ans) - 1:
            print(', ', end='')

    print(']')
C#
using System;
using System.Collections.Generic;

public class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int val)
    {
        data = val;
        left = right = null;
    }
}

public class GFG {
    public static void recLeftView(Node root, int level,
                                   List<int> res)
    {
        if (root == null)
            return;

        // first node of current level
        if (level == res.Count) {
            res.Add(root.data);
        }

        recLeftView(root.left, level + 1, res);
        recLeftView(root.right, level + 1, res);
    }

    public static List<int> leftView(Node root)
    {
        List<int> res = new List<int>();
        recLeftView(root, 0, res);
        return res;
    }

    public static void Main()
    {

        // Tree:
        //          1
        //        /   \
        //       2     3
        //             /
        //           4
        //            \
        //             5

        Node root = new Node(1);

        root.left = new Node(2);
        root.right = new Node(3);

        root.right.left = new Node(4);

        root.right.left.right = new Node(5);

        List<int> ans = leftView(root);

        Console.Write('[');

        for (int i = 0; i < ans.Count; i++) {
            Console.Write(ans[i]);
            if (i != ans.Count - 1)
                Console.Write(", ");
        }

        Console.Write(']');
    }
}
JavaScript
class Node {
    constructor(val)
    {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

function recLeftView(root, level, res)
{
    if (root === null) {
        return;
    }

    // first node of current level
    if (level === res.length) {
        res.push(root.data);
    }

    recLeftView(root.left, level + 1, res);
    recLeftView(root.right, level + 1, res);
}

function leftView(root)
{
    let res = [];
    recLeftView(root, 0, res);
    return res;
}

// Tree:
//          1
//        /   \
//       2     3
//             /
//           4
//            \
//             5

let root = new Node(1);

root.left = new Node(2);
root.right = new Node(3);

root.right.left = new Node(4);

root.right.left.right = new Node(5);

let ans = leftView(root);

console.log("[");

for (let i = 0; i < ans.length; i++) {
    process.stdout.write(ans[i].toString());
    if (i !== ans.length - 1) {
        process.stdout.write(", ");
    }
}

console.log("]");

Output
[1, 2, 4, 5]
Comment