## Introduction

Letâ€™s assume a person is sitting at the left and wishes to view a tree. Nodes would block some of his views when the personâ€™s line of sight would be limited from just one direction. The nodes to be printed are the nodes in the tree visible when viewed from the left side. We need to find the approaches to find the left view of a binary tree.

Letâ€™s see how a tree would be visible to a person wishing to view a tree from the left.

So the nodes visible to a person viewing the tree from the left would be 10, 20, 30, 70, and 90. The left nodes would hide the rest of the nodes. We are asked for a technique to print the left view of the nodes as a result.

This problem can have multiple approaches. Letâ€™s go through them and understand which is the more optimal solution.

Recommended: Try the Problem yourself first before moving on to the solution

## Solving using Level Order Traversal

If we consider the above example, notice the leftmost(first) node in each level of the tree is the one that is considered in the final answer. This approach is also known as an â€śIterative Method.â€ť This method can be used to find the left view of a binary tree.

We are given that the tree has five levels; letâ€™s see all the nodes in Level Order from left to right.

So, to perform Level Order Traversal, the nodes would be considered level-wise. This means that each node would access the child at each level before moving to the next level.

We have these nodes respective to each level,

Level 0 - 10

Level 1 - 20, 50

Level 2 - 30, 40, 60, 100

Level 3 - 70, 80

Level 4 - 90

Notice the first node in each level is the node that is to be printed for the result.

### Algorithm

For this approach, to find the left view of a binary tree, we will use a queue for the traversal of the tree and check if the elements are present, and print them accordingly to each level of the tree. The approach is known as an iterative approach because, after the traversal of the nodes, a NULL pointer is assigned to mark the end of the level. To do the level order traversal, we then iterate from the first level to the last level and push the children of the nodes in the queue.

**Pseudocode:**

```
enqueue root
enqueue null
while queue is not empty // size not equal to zero
node = dequeue queue
if node is null
print the first node of the level
dequeue node
increase level
enqueue null again
// meaning one level of nodes already enqueued
else
enqueue left and right child of node
```

### Implementation in Java

```
import java.util.*;
public class Main {
public static class Node {
int data = 0;
Node left = null;
Node right = null;
Node(int data) {
this.data = data;
}
}
static int idx = 0;
public static Node create_Tree(int[] arr) {
if (idx == arr.length || arr[idx] == -1) {
idx++;
return null;
}
Node node = new Node(arr[idx++]);
node.left = create_Tree(arr);
node.right = create_Tree(arr);
return node;
}
public static void leftView(Node node) {
LinkedList<Node> que = new LinkedList<>();
que.addLast(node);
while (que.size() != 0) {
int size = que.size();
System.out.print(que.getFirst().data + " ");
while (size-- > 0) {
Node rnode = que.removeFirst();
if (rnode.left != null)
que.addLast(rnode.left);
if (rnode.right != null)
que.addLast(rnode.right);
}
}
System.out.println();
}
public static void viewSet(Node root) {
leftView(root);
}
public static void solve() {
int[] arr = { 10, 20, 30, -1, -1, 40, -1, -1, 50, 60, 70, -1, -1, 80, 90, -1, -1, -1, 100, -1, -1 };
Node root = create_Tree(arr);
viewSet(root);
}
public static void main(String[] args) {
solve();
}
}
```

OUTPUT

`10 20 30 70 90 `

**Time Complexity: **The time complexity of the algorithm to find the left view of a binary tree using level order traversal is **O(N)** where N is the number of nodes in the Binary Tree.

**Space Complexity: **The space complexity of the algorithm is **O(N)**.

Read More - __Time Complexity of Sorting Algorithms__