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 firstname.lastname@example.org. 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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Print Right View of a Binary Tree
- Print all k-sum paths in a binary tree
- Print all the Paths of a Binary Tree whose XOR is non-zero
- Print all K-sum levels in a Binary Tree
- Print all nodes in a binary tree having K leaves
- Print all Exponential Levels of a Binary Tree
- Print cousins of a given node in Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print all internal nodes of a Binary tree
- Print path between any two nodes in a Binary Tree
- Print Bottom-Right View of a Binary Tree
- Print Palindromic Paths of Binary tree
- Print all Coprime path of a Binary Tree
- Print path between any two nodes in a Binary Tree | Set 2
- Print Binary Search Tree in Min Max Fashion
- Print all Co-Prime Levels of a Binary Tree
- Print Left View of a Binary Tree
- Print Nodes in Top View of Binary Tree
- Print Ancestors of a given node in Binary Tree