Given a Complete Binary Tree rooted at node 1, the task is to print the elements in the following defined order.
- First, print all elements of the last level in an alternate way such as first you print leftmost element and then rightmost element & continue in this until all elements are traversed of last level.
- Now do the same for the rest of the levels.
Input: 1 / \ 2 3 / \ / 4 5 6 Output: 4 6 5 2 3 1 Explanation: First print all elements of the last level which will be printed as follows: 4 6 5 Now tree becomes 1 / \ 2 3 Now print elements as 2 3 Now the tree becomes: 1 Input: 1 / \ 2 3 Output: 2 3 1
- Make a bfs call and store all the nodes present at level i int a vector array.
- Also keep track of maximum level reached in a bfs call.
- Now print the desired pattern starting from max level to 0
Below is the implementation of the above approach:
4 6 5 2 3 1
- Level order traversal with direction change after every two levels
- Level order traversal with direction change after every two levels | Recursive Approach
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Sum of all the levels in a Binary Search Tree
- Print the nodes at odd levels of a tree
- Averages of Levels in Binary Tree
- Print all nodes between two given levels in Binary Tree
- Maximum sum from a tree with adjacent levels not allowed
- Print Levels of all nodes in a Binary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Number of edges in a perfect binary tree with N levels
- Reverse alternate levels of a perfect binary tree
- Print Binary Tree levels in sorted order | Set 2 (Using set)
- Print Binary Tree levels in sorted order
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.