This post is completed by 1 user

  • 0
Add to List
Medium

64. Print Left View of binary tree

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

What is left View of a binary Tree?

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

Example:

Left View of a binary tree

Using Preorder Traversal

  • The idea is to preorder traversal ( root, left, right ) and print the first node you encounter at each level.
  • To implement the above
    • Take two variables, currentLevel = 0 and nextLevel=0
    • Every time you go left or right, nextLevel by 1. 
    • If current level<nextLevel, print the node and set currentLevel = nextLevel. This way you will print only the first node you encounter at each level
 
5 10 20 45

Breadth-First Search: Do the Level order traversal and print the first node value

 
5 10 20 45