# Left Hand Projection of a Tree

## Introduction

Another interesting interview question, this question has many forms. One of the form is print the left hand projection of a tree or right hand projection of a tree or so on. Also, this problem can be specialized for a binary tree, ternary tree or n-ary tree, it really doesn’t matter, the concept is the same.

We will just try and print the left hand projection and I will add a note on how to print the right had project and you can try that yourself.

## Understanding the problem – Left Hand Projection of a Tree

Let us consider the below tree for our problem statement.

##### What is Left Hand Projection of a Tree?

The left hand projection means the nodes which we can see if we look at the tree from the left hand side. This translates to the following:

• We have to pick up one node at each level.
• The node is the first one on that level.
• The number of nodes in the left hand projection would be equal to the height of the tree (because we take one node per level).

For the above tree the left hand projection would be : 1, 2, 6, 13, 15, 18

##### What is Right Hand Projection of a Tree?

The right hand projection means the nodes which we can see if we look at the tree from the right hand side. This translates to the following:

• We have to pick up one node at each level.
• The node is the last one on that level.
• The number of nodes in the right hand projection would be equal to the height of the tree (because we take one node per level).

For the above tree the right hand projection would be : 1, 5, 12, 14, 17, 19

Note: If the tree has one node per level then the left hand projection is exactly same as the right hand projection.

## Solution

The idea for the solution is similar to the one for Level Order Traversal. It’s just that at each level we are only interested in the first or the last node.

##### Pseudo Code

Similarly you can write the pseudo code for the right hand projection.

## Source Code – Left Hand Projection of a Tree

Here is the source code for the n-ary tree, if you want to do it for a binary tree, you can replace the `node.getChildren` with `node.getRight` and `node.getLeft`.