Write an efficient algorithm to print the two-dimensional view of a binary tree.

Print Binary Tree

For example, the above binary tree can be displayed as:


                     7
 
         3
 
                     6
 
 1
 
                     5
 
         2
 
                     4

 
One can notice the following things in the above binary tree illustration:

  1. The rightmost node is printed in the first line.
  2. The leftmost node is printed in the last line.
  3. A fixed amount of space is increased at each level.

Consider the following C++, Java, and Python code, which performs a reverse inorder traversal and print nodes by increasing space by a fixed amount at every level. It serves as an excellent utility function for printing the binary tree.

C++


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Author: Aditya Goel