This post is completed by 1 user
|Add to List|
Print Left View of a given binary tree
Objective: In a Binary Tree, print the left view of it
Input: A binary tree.
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.
- 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 the rest of the nodes on the level currentLevel and nextLevel are equal so it won't print
Do the Level order traversal and print the first node value
METHOD 1: 5 10 20 45 METHOD 2 : Using Level Order, Left view 5 10 20 45