Given a Complete Binary Tree, the task is to print the elements in the following pattern. Let’s consider the tree to be:
The tree is traversed in the following way:
The output for the above tree is:
1 3 7 11 10 9 8 4 5 6 2
Approach: The idea is to use the modified breadth first search function to store all the nodes at every level in an array of vector. Along with it, the maximum level up to which the tree needs to be traversed is also stored in a variable. After this precomputation task, the following steps are followed to get the required answer:
- Create a vector tree where tree[i] will store all the nodes of the tree at the level i.
- Take an integer variable k which keeps the track of the level number that is being traversed and another integer variable path which keeps the track of the number of cycles that have been completed. A flag variable is also created to keep the track of the direction in which the tree is being traversed.
- Now, start printing the rightmost nodes at each level until the maximum level is reached.
- Since the maximum level is reached, the direction has to be changed. In the last level, print elements from rightmost to left. And the value of maxLevel variable has to be decremented.
- As the tree is being traversed from the lower level to the upper level, the rightmost elements are printed. Since in the next iteration, the maxlevel value has been changed, it makes sure that already visited nodes in the last level are not traversed again.
The above steps are repeated until the entire tree is traversed.
Below is the implementation of the above approach:
1 3 7 11 10 9 8 4 5 6 2
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.
- Iterative Boundary Traversal of Complete Binary tree
- Level order traversal of Binary Tree using Morris Traversal
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Linked complete binary tree & its creation
- Find the largest Complete Subtree in a given Binary Tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Height of a complete binary tree (or Heap) with N nodes
- Print path from root to all nodes in a Complete Binary Tree
- Construct Complete Binary Tree from its Linked List Representation
- Number of edges in mirror image of Complete binary tree
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- Mix Order Traversal of a Binary Tree
- Density of Binary Tree in One Traversal
- Boundary Traversal of binary tree
- Diagonal Traversal of Binary Tree
- Zig-Zag traversal of a Binary Tree using Recursion
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.