Given a Binary Tree, print it in two dimension.
Input : Pointer to root of below tree 1 / \ 2 3 / \ / \ 4 5 6 7 Output : 7 3 6 1 5 2 4
We strongly recommend you to minimize your browser and try this yourself first.
If we take a closer look at the pattern, we can notice following.
1) Rightmost node is printed in first line and leftmost node is printed in last line.
2) Space count increases by a fixed amount at every level.
So we do a reverse inorder traversal (right – root – left) and print tree nodes. We increase space by a fixed amount at every level.
Below is the implementation.
15 7 14 3 13 6 12 1 11 5 10 2 9 4 8
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print all K-sum levels in a Binary Tree
- Print all k-sum paths in a binary tree
- Print Right View of a Binary Tree
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Print nodes in the Top View of Binary Tree | Set 3
- Print the nodes having exactly one child in a Binary tree
- Print all nodes in a binary tree having K leaves
- Print all nodes between two given levels in Binary Tree
- Print Nodes in Top View of Binary Tree
- Print Bottom-Right View of a Binary Tree
- Print the nodes of Binary Tree having a grandchild
- Print all Prime Levels of a Binary Tree
- Print Palindromic Paths of Binary tree
- Print all Exponential Levels of a Binary Tree
- Print all even nodes of Binary Search Tree
- Print nodes in top view of Binary Tree | Set 2
- Print cousins of a given node in Binary Tree
- Print Binary Search Tree in Min Max Fashion
- Print a Binary Tree in Vertical Order | Set 1