0
0
DSA Javascriptprogramming~20 mins

Morris Traversal Inorder Without Stack in DSA Javascript - 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

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.