Tag Archives: hrishikesh

Print all path for a binary tree

Print all path for a binary tree

For a binary tree print out all its root-to-leaf paths.

Solution:

  • Do BSF store child-parent map, of leaf node traverse back to root through this map.

Source Code:
Github: BinaryTreeLeafPathFinder.java


Output:

                         
               1                               
              / \               
             /   \              
            /     \             
           /       \            
          /         \           
         /           \          
        /             \         
       /               \        
       2               3               
      / \                       
     /   \                      
    /     \                     
   /       \                    
   4       5                       
  /       /                     
 /       /                      
 8       6                       
          \                     
          7                     
                                                                

Path for leaf : 3 :: 3 => 1 => 
Path for leaf : 8 :: 8 => 4 => 2 => 1 => 
Path for leaf : 7 :: 7 => 6 => 5 => 2 => 1 =>