Print a Binary Tree in Vertical Order

Print a Binary Tree in Vertical Order

Solution:

  • There could be two different solution
  • One is using sorted map, in which or every horizontal distance we all element
  • Second, multiple Iteration over tree with take extra data structure
    • First scan whole tree to find max and min distance from root
    • Again, scan whole tree and print base on vertical level
Algorithm for second solution:
  • Iterate whole tree to find min and max horizontal distance
  • Now iterate from min to max horizontal distance and print node of tree

Latest Source Code:
Github: BinaryTreeVerticalOrderPrinter.java


Output:

       1               
      / \       
     /   \      
    /     \     
   /       \    
   2       3       
  / \     / \   
 /   \   /   \  
 4   5   6   7   
          \   \ 
          8   9 
                                
Vertical Printing : 
4 
2 
1 5 6 
3 8 
7 
9 

Author: Hrishikesh Mishra