Input: 1 / \ 2 3 Output: 1 2 3 Input: 5 / \ 2 3 \ 6 Output: 5 2 3 6
Approach: The idea is to use Morris Preorder Traversal to traverse the tree in level order traversal.
Observations: There are mainly two observations for the traversal of the tree using Morris preorder traversal. That is –
- In Preorder traversal the left-most nodes of a level are visited first due which it can be used to traverse the tree in level order fashion.
- As we maintain the horizontal distance of the nodes in the top view of the binary tree, Similarly If we maintain the level of the current node and increment or decrement the level accordingly as per the movement, Then the nodes can be traversed easily.
As in the Morris preorder traversal, we connect the right-most node of the left child to its inorder successor to maintain the movement such that we can traverse back to the right child of the parent node after completely exploring the left child of the parent. Therefore, while moving to the rightmost child of the left child we can keep track of the number of the increment in the level to compute the level inorder successor of the child.
Below is the implementation of the above approach:
5 2 3 6
- Time Complexity: As in the above approach, every node is touched at max twice due to which the time complexity is O(N), where N is the number of nodes.
- Auxiliary Space: As in the above approach, there is no extra space used due to which auxiliary space used will be O(1)
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.
- Reverse Morris traversal using Threaded Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Flatten Binary Tree in order of Level Order Traversal
- Morris traversal for Preorder
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Insertion in n-ary tree in given order and Level order traversal
- Density of Binary Tree using Level Order Traversal
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Connect Nodes at same Level (Level Order Traversal)
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Build Binary Tree from BST such that it's level order traversal prints sorted data
- Check if the level order traversal of a Binary Tree results in a palindrome
- Boundary Level order traversal of a Binary Tree
- Specific Level Order Traversal of Binary Tree
- Zig Zag Level order traversal of a tree using single array
- Flatten binary tree in order of post-order traversal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.