Morris traversal for Preorder
Morris traversal is a way to traverse a binary tree in-order without using recursion or a stack. In Morris traversal, we make use of the right child pointer of each node to navigate through the tree in a specific manner.
When we talk about Morris traversal for pre-order, we are referring to a specific way of using Morris traversal to perform pre-order traversal of a binary tree. In this approach, we visit the node first, then traverse the left subtree and finally traverse the right subtree.
The steps involved in Morris traversal for pre-order are:
- Initialize the current node as the root of the binary tree.
- While the current node is not null:
- If the left child of the current node is null, visit the current node and move to its right child.
- If the left child of the current node is not null, find the rightmost node in the left subtree of the current node.
- If the right child of this rightmost node is null, set it to the current node, visit the current node and move to its left child.
- If the right child of this rightmost node is not null, set it to null and move to the right child of the current node.
By following these steps, we can traverse the binary tree in pre-order using Morris traversal without using recursion or a stack.
-
1) Morris preorder traversal in java
2) Morris preorder traversal in c++
3) Morris preorder traversal in c
4) Morris preorder traversal in golang
5) Morris preorder traversal in c#
6) Morris preorder traversal in vb.net
7) Morris preorder traversal in php
8) Morris preorder traversal in node js
9) Morris preorder traversal in typescript
10) Morris preorder traversal in python
11) Morris preorder traversal in ruby
12) Morris preorder traversal in scala
13) Morris preorder traversal in kotlin
14) Morris preorder traversal in swift
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment