Open In App

Left View of a Binary Tree

Last Updated : 07 Oct, 2025
Comments
Improve
Suggest changes
263 Likes
Like
Report

Given the root of a binary tree, Find its left view.

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

  • It contains the leftmost node at each level, starting from the root (top level) down to the deepest level.
  • Return the nodes in order from the top level to the bottom level.

Examples

Input:

binary_tree_to_string_with_brackets

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

file

Input:

420046674


Output: [1, 2, 4, 5]
Explanation: The first node from each level will be the part of left view of tree.

420046675

[Approach - 1] Using Depth-first search (DFS) - O(n) Time and O(n) Space

The idea is to use DFS and Keep track of current level. For every level of the binary tree, the first node we see from the left side is part of the left view.

C++
#include <iostream>
#include <vector>
using namespace std;

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

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

// Recursive function to find left view
void recLeftView(Node* root, int level, vector<int>& result) {
    if (root == nullptr) return;

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

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

// Function which return left view of binary tree
vector<int> leftView(Node* root) {
    vector<int> result;
    recLeftView(root, 0, result);
    return result;
}

int main() {
    // Create binary 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> view = leftView(root);
    for (int val : view)
        cout << val << " ";
}
Java
import java.util.ArrayList;

// Node Structure
class Node {
    int data;
    Node left, right;

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

class GFG {
    
    // Recursive function to find left view
    static void recLeftView(Node root, int level, ArrayList<Integer> result) {
        if (root == null) return;
        
        // first node of current level
        if (level == result.size()) {
            result.add(root.data);
        }
        
        recLeftView(root.left, level + 1, result);
        recLeftView(root.right, level + 1, result);
    }

    // Function which return left view of binary tree
    static ArrayList<Integer> leftView(Node root) {
        ArrayList<Integer> result = new ArrayList<>();
        recLeftView(root, 0, result);
        return result;
    }

    public static void main(String[] args) {
      
        // Create binary 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> view = leftView(root);
        for (int val : view) 
            System.out.print(val + " ");

    }
}
Python
# Node Structure
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None
        
# Recursive function to find left view
def recLeftView(root, level, result):
    if root is None:
        return
    
    # first node of current level
    if level == len(result):
        result.append(root.data)
    
    recLeftView(root.left, level + 1, result)
    recLeftView(root.right, level + 1, result)

# Function which return left view of binary tree
def leftView(root):
    result = []
    recLeftView(root, 0, result)
    return result
    
if __name__ == "__main__":
    #  Create binary 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)

    result = leftView(root)
    print(*result);
C#
using System;
using System.Collections.Generic;

// Node Structure
class Node {
    public int data;
    public Node left, right;

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

class GFG {
    
    // Recursive function to find left view
    static void recLeftView(Node root, int level, List<int> result) {
        if (root == null) return;
        
        // first node of current level
        if (level == result.Count) {
            result.Add(root.data);
        }
        
        recLeftView(root.left, level + 1, result);
        recLeftView(root.right, level + 1, result);
    }

    // Function which return left view of binary tree
    static List<int> leftView(Node root) {
        List<int> result = new List<int>();
        recLeftView(root, 0, result);
        return result;
    }
    
    static void Main(string[] args) {
        // Create binary 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> view = leftView(root);
        foreach (int val in view) {
            Console.Write(val + " ");
        }
    }
}
JavaScript
// Node Structure
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Recursive function to find left view
function recLeftView(root, level, result) {
    if (root === null) return;
    
        // first node of current level
    if (level === result.length) {
        result.push(root.data);
    }
    
    recLeftView(root.left, level + 1, result);
    recLeftView(root.right, level + 1, result);
}

// Function which return left view of binary tree
function leftView(root) {
    const result = [];
    recLeftView(root, 0, result);
    return result;
}

// Driver Code

// Create binary tree
//    1
//   / \
//  2   3
//     /
//    4
//     \
//      5
    
const 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);

const result = leftView(root);
console.log(...result);

Output
1 2 4 5 

[Approach - 2] Using Level Order Traversal (BFS) - O(n) Time and O(n) Space

The left view contains all nodes that are the first nodes in their levels. A simple solution is to do level order traversal and print the first node in every level.

C++
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

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

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

vector<int> leftView(Node* root) {
    if (root == nullptr) return {};

    // Queue for level order traversal
    queue<Node*> q;
    vector<int> res;
    q.push(root);

    while (!q.empty()) {
        int levelSize = q.size();

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

            // If it's the first node of the current level
            if (i == 0)
                res.push_back(curr->data);

            if (curr->left != nullptr)
                q.push(curr->left);
            if (curr->right != nullptr)
                q.push(curr->right);
        }
    }
    return res;
}

int main() {
    // Create binary 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> res = leftView(root);
    for (int node : res)
        cout << node << " ";
}
Java
import java.util.ArrayList;
import java.util.Queue;
import java.util.LinkedList;

// Node Structure
class Node {
    int data;
    Node left, right;

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

class GFG {
    static ArrayList<Integer> leftView(Node root) {
        if (root == null) return new ArrayList<>();

        // Queue for level order traversal
        Queue<Node> q = new LinkedList<>();
        ArrayList<Integer> res = new ArrayList<>();
        q.add(root);

        while (!q.isEmpty()) {
            int levelSize = q.size();

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

                // If it's the first node of the current level
                if (i == 0) 
                    res.add(curr.data);
                
                if (curr.left != null) {
                    q.add(curr.left);
                }
                
                if (curr.right != null) {
                    q.add(curr.right);
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        // Create binary 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> res = leftView(root);
        for( int node : res )
            System.out.print(node + " ");
    }
}
Python
from collections import deque

# Node Structure
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None

def leftView(root):
    if root is None:
        return []

    # Queue for level order traversal
    q = deque()
    res = []
    q.append(root)

    while q:
        levelSize = len(q)

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

            # If it's the first node of the current level
            if i == 0:
                res.append(curr.data)

            if curr.left:
                q.append(curr.left)
            if curr.right:
                q.append(curr.right)

    return res

if __name__ == "__main__" :
    #  Create binary 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)
    
    res =leftView(root)
    print(*res)
C#
using System;
using System.Collections.Generic;

// Node Structure
class Node {
    public int data;
    public Node left, right;

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

class GFG {
    static List<int> leftView(Node root) {
        if (root == null) 
            return new List<int>();  

        // Queue for level order traversal
        Queue<Node> q = new Queue<Node>();
        List<int> res = new List<int>();
        q.Enqueue(root);

        while (q.Count > 0) {
            int levelSize = q.Count;

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

                // If it's the first node of the current level
                if (i == 0)
                    res.Add(curr.data);

                if (curr.left != null) q.Enqueue(curr.left);
                if (curr.right != null) q.Enqueue(curr.right);
            }
        }
        return res;
    }

    static void Main(string[] args) {
        // Create binary 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> res = leftView(root);
        foreach (int node in res)
            Console.Write(node + " ");
    }
}
JavaScript
// Node Structure
class Node {
    constructor(x) {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Custom Queue Implementation
class Queue {
    constructor() {
        this.items = {};
        this.headIndex = 0;
        this.tailIndex = 0;
    }

    enqueue(item) {
        this.items[this.tailIndex] = item;
        this.tailIndex++;
    }

    dequeue() {
        if (this.isEmpty()) return null;
        let item = this.items[this.headIndex];
        delete this.items[this.headIndex];
        this.headIndex++;
        return item;
    }

    isEmpty() {
        return this.tailIndex === this.headIndex;
    }

    size() {
        return this.tailIndex - this.headIndex;
    }
}

function leftView(root) {
    if (!root) return [];
   
     // Queue for level order traversal
    let q = new Queue();
    let res = [];
    q.enqueue(root);

    while (!q.isEmpty()) {
        let levelSize = q.size();

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

            // If it's the first node of the current level
            if (i === 0) res.push(curr.data);

            if (curr.left) q.enqueue(curr.left);
            if (curr.right) q.enqueue(curr.right);
        }
    }
    return res;
}

// Driver code

// Create binary 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 res = leftView(root);
console.log(res.join(" ")); 

Output
1 2 4 5 

Print Left View of Binary Tree

Explore