0
0
DSA C++programming~20 mins

Morris Traversal Inorder Without Stack in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Morris Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Morris Inorder Traversal on a simple tree
What is the output of the following Morris inorder traversal code on the given binary tree?
DSA C++
struct Node {
    int data;
    Node* left;
    Node* right;
};

// Tree structure:
//       2
//      / \
//     1   3

void morrisInorder(Node* root) {
    Node* current = root;
    while (current != nullptr) {
        if (current->left == nullptr) {
            std::cout << current->data << " ";
            current = current->right;
        } else {
            Node* pre = current->left;
            while (pre->right != nullptr && pre->right != current) {
                pre = pre->right;
            }
            if (pre->right == nullptr) {
                pre->right = current;
                current = current->left;
            } else {
                pre->right = nullptr;
                std::cout << current->data << " ";
                current = current->right;
            }
        }
    }
}

// Call morrisInorder(root) where root is the node with data=2
A3 2 1
B2 1 3
C1 2 3
D1 3 2
Attempts:
2 left
💡 Hint
Remember inorder traversal visits left subtree, then node, then right subtree.
Predict Output
intermediate
2:00remaining
Output of Morris Traversal on a tree with left skew
What is the output of the Morris inorder traversal on this left skewed tree?
DSA C++
// Tree structure:
//     3
//    /
//   2
//  /
// 1

// Using the same morrisInorder function as before, called with root = node with data=3
A3 2 1
B1 3 2
C2 1 3
D1 2 3
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree first, so the smallest value comes first.
🔧 Debug
advanced
2:00remaining
Identify the error in this Morris traversal code snippet
What error will this Morris traversal code produce when run on a binary tree?
DSA C++
void morrisInorder(Node* root) {
    Node* current = root;
    while (current != nullptr) {
        if (current->left == nullptr) {
            std::cout << current->data << " ";
            current = current->right;
        } else {
            Node* pre = current->left;
            while (pre->right != nullptr) {
                pre = pre->right;
            }
            if (pre->right == nullptr) {
                pre->right = current;
                current = current->left;
            } else {
                pre->right = nullptr;
                std::cout << current->data << " ";
                current = current->right;
            }
        }
    }
}
AInfinite loop
BSegmentation fault
CCompilation error
DCorrect output
Attempts:
2 left
💡 Hint
Check the inner while loop condition for finding predecessor.
🧠 Conceptual
advanced
2:00remaining
Why does Morris Traversal not use stack or recursion?
Which of the following best explains why Morris traversal can perform inorder traversal without using stack or recursion?
AIt temporarily modifies the tree by creating threads to predecessors to remember the path back.
BIt uses a hidden stack inside the CPU registers to store nodes.
CIt uses a queue to store nodes instead of a stack.
DIt performs traversal by visiting nodes in random order and sorting them later.
Attempts:
2 left
💡 Hint
Think about how the algorithm remembers where to return after visiting left subtree.
🚀 Application
expert
2:00remaining
Number of nodes visited twice in Morris Traversal on a left-skewed tree
In Morris inorder traversal of a left-skewed binary tree with N nodes, how many nodes are visited exactly twice during the traversal?
AN
BN - 1
CN + 1
D0
Attempts:
2 left
💡 Hint
Consider how many temporary threads are created and removed during traversal.