Print Left View of a given binary tree

Objective: In a Binary Tree, print left view of it

Input: A binary tree.

What is left View of a binary Tree

When just look at the tree from the left side , all the nodes you can see will be the left view of the tree.


Left View of a binary tree


Method 1:

  • Traverse the tree from left to right
  • Print the first node you encounter
  • Take two variables , currentLevel=0 and nextLevel=1
  • As soon as you change level , change the currentLevel = nextLevel
  • Print only when current level<nextLevel so this way you will print only the first element
  • For rest of the nodes on the the level currentLevel and nextLevel are equal so it wont print

Method 2:

Do the Level order traversal and print the first node value

Complete Code:


5 10 20 45
METHOD 2 : Using Level Order, Left view
5 10 20 45

Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

You may also like...

1 Response

  1. Pintu Das says:

    Please check the height(Node root) method. I think the base case (root == null) should return -1 instead of 0;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: