Left View of Binary Tree

Left View of Binary Tree

Given a Binary Tree, print Left view of it. Left view of a Binary Tree is set of nodes visible when tree is visited from Left side.

Solution:

  • Try to go left whenever possible otherwise go to right.
Algorithm:
  • Traverse in modified preOrder, in this.
    • If node node is null
      • return
    • Print node
    • If node has left child then
      • recursively call for left child
    • If node has right child then
      • recursively call for right child

Latest Source Code:
Github: BinaryTreeLeftView.java


Output:

                               1                                                               
                              / \                               
                             /   \                              
                            /     \                             
                           /       \                            
                          /         \                           
                         /           \                          
                        /             \                         
                       /               \                        
                      /                 \                       
                     /                   \                      
                    /                     \                     
                   /                       \                    
                  /                         \                   
                 /                           \                  
                /                             \                 
               /                               \                
               2                               7                               
                \                             / \               
                 \                           /   \              
                  \                         /     \             
                   \                       /       \            
                    \                     /         \           
                     \                   /           \          
                      \                 /             \         
                       \               /               \        
                       12               3               13               
                      /                                         
                     /                                          
                    /                                           
                   /                                            
                   22                                               
                  / \                                           
                 /   \                                          
                 30   32                                           
                  \                                             
                  45                                             
                                                                                                                                


Left side view: 1 2 12 22 30 45 
Author: Hrishikesh Mishra