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.
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- 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
- 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
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Construct Complete Binary Tree from its Linked List Representation
- Height of a complete binary tree (or Heap) with N nodes
- Find the largest Complete Subtree in a given Binary Tree
- Print path from root to all nodes in a Complete Binary Tree
- Sum of the mirror image nodes of a complete binary tree in an inorder way
- 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
- Check if value exists in level-order sorted complete binary tree
- Print updated levels of each node of a Complete Binary Tree based on difference in weights of subtrees
- Construct a complete binary tree from given array in level order fashion
- Linked complete binary tree & its creation
- Cartesian tree from inorder traversal | Segment Tree
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.