Challenge - 5 Problems
Morris Traversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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.
Tree structure:
2
/ \
1 3
Assume the traversal prints node values separated by spaces.
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class MorrisTraversal { public static void morrisInorder(Node root) { Node current = root; while (current != null) { if (current.left == null) { System.out.print(current.val + " "); current = current.right; } else { Node 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; System.out.print(current.val + " "); current = current.right; } } } } public static void main(String[] args) { Node root = new Node(2); root.left = new Node(1); root.right = new Node(3); morrisInorder(root); } }
Attempts:
2 left
💡 Hint
Morris traversal visits nodes in inorder sequence without using stack or recursion.
✗ Incorrect
Morris inorder traversal visits nodes in the order: left subtree, root, right subtree. For the tree 2 with left child 1 and right child 3, the inorder is 1 2 3.
❓ Predict Output
intermediateMorris Traversal Output on Left-Skewed Tree
What is the output of the Morris inorder traversal on this left-skewed binary tree?
Tree structure:
4
/
3
/
2
/
1
Assume the traversal prints node values separated by spaces.
Tree structure:
4
/
3
/
2
/
1
Assume the traversal prints node values separated by spaces.
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class MorrisTraversal { public static void morrisInorder(Node root) { Node current = root; while (current != null) { if (current.left == null) { System.out.print(current.val + " "); current = current.right; } else { Node 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; System.out.print(current.val + " "); current = current.right; } } } } public static void main(String[] args) { Node root = new Node(4); root.left = new Node(3); root.left.left = new Node(2); root.left.left.left = new Node(1); morrisInorder(root); } }
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes from smallest to largest in a BST.
✗ Incorrect
The inorder traversal visits nodes in ascending order: 1 2 3 4 for this left-skewed tree.
🔧 Debug
advancedIdentify the Bug in Morris Traversal Implementation
The following code attempts Morris inorder traversal but produces incorrect output or infinite loop. What is the bug?
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class MorrisTraversal { public static void morrisInorder(Node root) { Node current = root; while (current != null) { if (current.left == null) { System.out.print(current.val + " "); current = current.right; } else { Node pre = current.left; while (pre.right != null) { pre = pre.right; } if (pre.right == null) { pre.right = current; current = current.left; } else { pre.right = null; System.out.print(current.val + " "); current = current.right; } } } } }
Attempts:
2 left
💡 Hint
Check the condition in the inner while loop that finds the predecessor.
✗ Incorrect
The inner while loop must check pre.right != current to avoid infinite loop by detecting threaded links.
🧠 Conceptual
advancedWhy Morris Traversal Uses Threaded Links?
Why does Morris inorder traversal create temporary links (threaded links) between nodes during traversal?
Attempts:
2 left
💡 Hint
Think about how Morris traversal avoids extra memory usage.
✗ Incorrect
Threaded links allow traversal to return to a node after visiting its left subtree without stack or recursion.
❓ Predict Output
expertOutput 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
Assume the traversal prints node values separated by spaces.
Tree structure:
5
/ \
3 7
/ \ \
2 4 8
/
1
Assume the traversal prints node values separated by spaces.
DSA Java
class Node { int val; Node left, right; Node(int v) { val = v; left = right = null; } } public class MorrisTraversal { public static void morrisInorder(Node root) { Node current = root; while (current != null) { if (current.left == null) { System.out.print(current.val + " "); current = current.right; } else { Node 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; System.out.print(current.val + " "); current = current.right; } } } } public static void main(String[] args) { Node 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); morrisInorder(root); } }
Attempts:
2 left
💡 Hint
Inorder traversal visits nodes in ascending order for BST.
✗ Incorrect
The inorder traversal visits nodes in sorted order: 1 2 3 4 5 7 8. Node 6 does not exist.
