0
0
DSA Typescriptprogramming~20 mins

Morris Traversal Inorder Without Stack in DSA Typescript - 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 Morris inorder traversal on the following binary tree?

Tree structure:
2
/ \
1 3

Assume the traversal prints node values separated by spaces.
DSA Typescript
class Node {
  val: number;
  left: Node | null;
  right: Node | null;
  constructor(val: number) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function morrisInorder(root: Node | null): void {
  let current = root;
  while (current !== null) {
    if (current.left === null) {
      process.stdout.write(current.val + ' ');
      current = current.right;
    } else {
      let pre = current.left;
      while (pre.right !== null && pre.right !== current) {
        pre = pre.right;
      }
      if (pre.right === null) {
        pre.right = current;
        current = current.left;
      } else {
        pre.right = null;
        process.stdout.write(current.val + ' ');
        current = current.right;
      }
    }
  }
}

const root = new Node(2);
root.left = new Node(1);
root.right = new Node(3);
morrisInorder(root);
A1 2 3
B2 1 3
C3 2 1
D1 3 2
Attempts:
2 left
💡 Hint
Remember inorder traversal visits left subtree, then node, then right subtree.
🧠 Conceptual
intermediate
1:30remaining
Understanding Thread Creation in Morris Traversal
In Morris inorder traversal, what is the purpose of creating a temporary thread (link) from the inorder predecessor to the current node?
ATo permanently modify the tree structure for faster future traversals
BTo avoid using recursion or stack by temporarily linking back to the current node after visiting left subtree
CTo store node values in a separate array for output
DTo mark nodes as visited to prevent revisiting
Attempts:
2 left
💡 Hint
Think about how Morris traversal avoids extra memory usage.
🔧 Debug
advanced
2:00remaining
Identify the Error in Modified Morris Traversal Code
What error will occur when running this modified Morris inorder traversal code snippet?

Code snippet:
let current = root;
while (current !== null) {
if (current.left !== null) {
let pre = current.left;
while (pre.right !== null && pre.right !== current) {
pre = pre.right;
}
if (pre.right === null) {
pre.right = current;
current = current.left;
} else {
pre.right = null;
console.log(current.val);
current = current.right;
}
} else {
console.log(current.val);
current = current.right;
}
}

Note: The condition in the first if is reversed from standard Morris traversal.
AInfinite loop due to missing thread creation when left is null
BCorrect output with no errors
CSyntaxError because of missing semicolons
DTypeError because current.left might be undefined
Attempts:
2 left
💡 Hint
Check what happens when current.left is null in this code.
📝 Syntax
advanced
1:30remaining
Identify Syntax Error in Morris Traversal Implementation
Which option contains a syntax error that prevents the Morris inorder traversal code from running?
Aif (pre.right === null) { pre.right = current; current = current.left; } else { pre.right = null; console.log(current.val); current = current.right; }
Bwhile (pre.right !== null && pre.right !== current) pre = pre.right;
Cclass Node { val: number; left: Node | null; right: Node | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } }
D
let current = root
while (current !== null) { if (current.left === null) { console.log(current.val); current = current.right } else { let pre = current.left; while (pre.right !== null && pre.right !== current) { pre = pre.right } if (pre.right === null) { pre.right = current; current = current.left; } else { pre.right = null; console.log(current.val); current = current.right } } }
Attempts:
2 left
💡 Hint
Look for missing semicolons or braces in the code.
🚀 Application
expert
2:00remaining
Number of Temporary Threads Created in Morris Traversal
Given a binary tree with 5 nodes arranged as:
4
/ \
2 5
/ \
1 3

How many temporary threads (links) will Morris inorder traversal create during the full traversal?
A4
B2
C3
D5
Attempts:
2 left
💡 Hint
Count how many nodes have a left child and thus create a thread to their inorder predecessor.