Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 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

Code snippet uses Morris traversal to print node values in order.
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function morrisInorder(root) {
  let result = [];
  let current = root;
  while (current !== null) {
    if (current.left === null) {
      result.push(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;
        result.push(current.val);
        current = current.right;
      }
    }
  }
  return result;
}

const root = new Node(2);
root.left = new Node(1);
root.right = new Node(3);
console.log(morrisInorder(root));
A[3, 2, 1]
B[1, 2, 3]
C[1, 3, 2]
D[2, 1, 3]
Attempts:
2 left
💡 Hint
Morris traversal prints nodes in inorder sequence without recursion or stack.
❓ Predict Output
intermediate
2:00remaining
Output of Morris Inorder Traversal on a Left-Skewed Tree
What is the output of the Morris inorder traversal on this left-skewed binary tree?

Tree structure:
4
/
3
/
2
/
1
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function morrisInorder(root) {
  let result = [];
  let current = root;
  while (current !== null) {
    if (current.left === null) {
      result.push(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;
        result.push(current.val);
        current = current.right;
      }
    }
  }
  return result;
}

const root = new Node(4);
root.left = new Node(3);
root.left.left = new Node(2);
root.left.left.left = new Node(1);
console.log(morrisInorder(root));
A[1, 2, 3, 4]
B[4, 3, 2, 1]
C[1, 3, 2, 4]
D[2, 1, 3, 4]
Attempts:
2 left
💡 Hint
Inorder traversal visits leftmost nodes first.
🔧 Debug
advanced
3:00remaining
Identify the Error in Morris Traversal Implementation
The following code attempts Morris inorder traversal but produces incorrect output. What is the bug causing this?

Code snippet:
function morrisInorder(root) {
  let result = [];
  let current = root;
  while (current !== null) {
    if (current.left === null) {
      result.push(current.val);
      current = current.left;  // Bug here
    } 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;
        result.push(current.val);
        current = current.right;
      }
    }
  }
  return result;
}
AThe code is missing a base case for empty tree
BThe inner while loop condition should check 'pre.left' instead of 'pre.right'
CLine 'current = current.left;' should be 'current = current.right;'
DThe result array should be initialized inside the while loop
Attempts:
2 left
💡 Hint
When left child is null, traversal should move to right child.
🧠 Conceptual
advanced
2:00remaining
Why Morris Traversal Uses Threaded Binary Tree Links
Why does Morris inorder traversal temporarily modify the tree by creating links (threads) from the rightmost node of the left subtree back to the current node?
ATo avoid using stack or recursion by remembering the path back to the current node
BTo permanently change the tree structure for faster future traversals
CTo balance the tree dynamically during traversal
DTo store node values temporarily for sorting after traversal
Attempts:
2 left
💡 Hint
Think about how Morris traversal avoids extra memory usage.
❓ Predict Output
expert
3:00remaining
Output of Morris Traversal on Complex Tree
What is the output of the Morris inorder traversal on this binary tree?

Tree structure:
5
/ \
3 7
/ \ \
2 4 8
/
1
DSA Javascript
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

function morrisInorder(root) {
  let result = [];
  let current = root;
  while (current !== null) {
    if (current.left === null) {
      result.push(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;
        result.push(current.val);
        current = current.right;
      }
    }
  }
  return result;
}

const root = new Node(5);
root.left = new Node(3);
root.right = new Node(7);
root.left.left = new Node(2);
root.left.right = new Node(4);
root.right.right = new Node(8);
root.left.left.left = new Node(1);
console.log(morrisInorder(root));
A[1, 3, 2, 4, 5, 7, 8]
B[1, 2, 3, 4, 5, 7, 8, 6]
C[5, 3, 2, 1, 4, 7, 8]
D[1, 2, 3, 4, 5, 7, 8]
Attempts:
2 left
💡 Hint
Inorder traversal visits left subtree, root, then right subtree.

Practice

(1/5)
1. What is the main advantage of Morris Traversal for inorder tree traversal compared to traditional methods?
easy
A. It only works on binary search trees
B. It is faster than all other traversal methods
C. It requires a queue to store nodes
D. It uses no extra space like stack or recursion

Solution

  1. Step 1: Understand Morris Traversal space usage

    Morris Traversal modifies tree links temporarily to avoid using stack or recursion, so it uses O(1) extra space.
  2. Step 2: Compare with traditional methods

    Traditional inorder traversal uses stack or recursion which requires O(h) space, where h is tree height.
  3. Final Answer:

    It uses no extra space like stack or recursion -> Option D
  4. Quick Check:

    Morris Traversal = O(1) space [OK]
Hint: Morris Traversal avoids stack and recursion for space savings [OK]
Common Mistakes:
  • Thinking Morris Traversal is faster in time complexity
  • Assuming it only works on binary search trees
  • Believing it uses extra data structures like queues
2. Which of the following is the correct way to find the inorder predecessor of a node curr in Morris Traversal?
easy
A. Move to the left child of curr and then keep moving right until right child is null or curr
B. Move to the right child of curr until null
C. Move to the left child of curr only once
D. Move to the right child of curr and then keep moving left until left child is null

Solution

  1. Step 1: Identify inorder predecessor in Morris Traversal

    The inorder predecessor is the rightmost node in the left subtree of curr.
  2. Step 2: Traverse left subtree then rightmost path

    Start at curr.left, move right repeatedly until right child is null or points back to curr.
  3. Final Answer:

    Move to the left child of curr and then keep moving right until right child is null or curr -> Option A
  4. Quick Check:

    Inorder predecessor = left subtree's rightmost node [OK]
Hint: Inorder predecessor = left child then rightmost path [OK]
Common Mistakes:
  • Moving right child first instead of left
  • Stopping after one left move without going right
  • Confusing predecessor with successor
3. What will be the output of Morris inorder traversal on the following binary tree?
    2
   / \
  1   3
Assuming the traversal prints node values in order.
medium
A. [1, 3, 2]
B. [2, 1, 3]
C. [1, 2, 3]
D. [3, 2, 1]

Solution

  1. Step 1: Understand inorder traversal order

    Inorder traversal visits left subtree, then current node, then right subtree.
  2. Step 2: Apply Morris traversal on given tree

    Left child is 1, root is 2, right child is 3, so output is [1, 2, 3].
  3. Final Answer:

    [1, 2, 3] -> Option C
  4. Quick Check:

    Inorder = left, root, right = [1, 2, 3] [OK]
Hint: Inorder prints left, root, right nodes [OK]
Common Mistakes:
  • Confusing preorder or postorder with inorder
  • Mixing node visit order
  • Assuming traversal prints in reverse
4. Consider this snippet of Morris Traversal code:
while (curr !== null) {
  if (curr.left === null) {
    console.log(curr.val);
    curr = curr.right;
  } else {
    let pred = curr.left;
    while (pred.right !== null && pred.right !== curr) {
      pred = pred.right;
    }
    if (pred.right === null) {
      pred.right = curr;
      curr = curr.left;
    } else {
      pred.right = null;
      console.log(curr.val);
      curr = curr.right;
    }
  }
}
What is the error in this code?
medium
A. It misses printing the current node after restoring links
B. It never prints the current node's value
C. It creates infinite loop by not updating curr
D. It incorrectly sets pred.right to curr.left

Solution

  1. Step 1: Check when node values are printed

    Values are printed only when curr.left === null, but after restoring links, printing is missing.
  2. Step 2: Identify missing print after restoring links

    After setting pred.right = null, the code moves curr = curr.right without printing curr.val.
  3. Final Answer:

    It misses printing the current node after restoring links -> Option A
  4. Quick Check:

    Print missing after restoring links = bug [OK]
Hint: Print node after restoring links to avoid missing values [OK]
Common Mistakes:
  • Not printing node after restoring predecessor link
  • Confusing when to update curr pointer
  • Incorrectly modifying tree structure
5. You want to modify Morris Traversal inorder to collect node values in an array instead of printing. Which change is necessary to correctly collect all nodes in order?
hard
A. Push curr.val only when curr.left === null
B. Push curr.val both when curr.left === null and after restoring links
C. Push curr.val only after restoring links
D. Push curr.val before moving to left child

Solution

  1. Step 1: Identify when nodes are visited in Morris Traversal

    Nodes are visited either when curr.left === null or after restoring the threaded link.
  2. Step 2: Collect values at both visit points

    To collect all nodes in inorder, push curr.val both when curr.left === null and after restoring links (before moving right).
  3. Final Answer:

    Push curr.val both when curr.left === null and after restoring links -> Option B
  4. Quick Check:

    Collect values at both visits = correct inorder [OK]
Hint: Collect node values at both visits for full inorder [OK]
Common Mistakes:
  • Collecting values only once misses nodes
  • Collecting before moving left causes wrong order
  • Ignoring second visit after restoring links
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard