Bird
Raised Fist0
DSA Javaprogramming~5 mins

Morris Traversal Inorder Without Stack in DSA Java - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the main idea behind Morris Traversal for inorder tree traversal?
Morris Traversal uses threaded binary trees by temporarily modifying the tree's right pointers to avoid using a stack or recursion, enabling inorder traversal with O(1) extra space.
Click to reveal answer
intermediate
In Morris Traversal, what do we do when the current node has a left child?
We find the rightmost node in the left subtree (predecessor). If its right pointer is null, we set it to point to the current node (threading) and move current to its left child. If the right pointer already points to current, we revert it to null and visit current.
Click to reveal answer
beginner
Why does Morris Traversal not require a stack or recursion?
Because it temporarily creates threads (links) to the inorder successor nodes, allowing traversal back to parent nodes without extra memory for stack or recursion.
Click to reveal answer
intermediate
What is the time complexity of Morris Traversal Inorder and why?
The time complexity is O(n) because each edge is visited at most twice: once to create a thread and once to remove it, ensuring linear time traversal.
Click to reveal answer
beginner
Show the output of Morris Inorder Traversal on this tree: 2 / \ 1 3
The inorder traversal output is: 1 -> 2 -> 3 -> null
Click to reveal answer
What does Morris Traversal use to avoid stack or recursion?
AQueue data structure
BExtra stack memory
CRecursion with memoization
DTemporary threads linking nodes
When the predecessor's right pointer is null in Morris Traversal, what should we do?
ASet it to current node and move current to left child
BVisit current node immediately
CMove current to right child
DDelete the predecessor node
What happens when the predecessor's right pointer points to current node?
ASkip current node
BRemove the thread and visit current node
CCreate a new thread
DMove current to left child
What is the space complexity of Morris Traversal?
AO(n)
BO(log n)
CO(1)
DO(n^2)
Which traversal order does Morris Traversal implement in this context?
AInorder
BPreorder
CPostorder
DLevel order
Explain step-by-step how Morris Traversal performs inorder traversal without using a stack or recursion.
Think about how the traversal moves left, creates links, and then visits nodes.
You got /6 concepts.
    Describe the advantages and possible drawbacks of using Morris Traversal for inorder traversal.
    Consider memory use and tree integrity.
    You got /6 concepts.

      Practice

      (1/5)
      1. What is the main advantage of Morris Traversal for inorder tree traversal compared to the traditional recursive or stack-based methods?
      easy
      A. It requires the tree to be balanced
      B. It is faster than all other traversal methods
      C. It only works on binary search trees
      D. It uses no extra space for 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 recursion or stack which require O(h) space, where h is tree height.
      3. Final Answer:

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

        Space complexity = O(1) [OK]
      Hint: Morris uses threaded links to avoid stack [OK]
      Common Mistakes:
      • Thinking Morris is faster in time complexity
      • Assuming it only works on BSTs
      • Believing it requires balanced trees
      2. Which of the following code snippets correctly creates the temporary thread link in Morris Traversal inorder method?
      easy
      A. current.right = predecessor;
      B. current.left = predecessor;
      C. predecessor.right = current;
      D. predecessor.left = current;

      Solution

      1. Step 1: Identify the thread link direction

        In Morris Traversal, the right child of the inorder predecessor is linked to current node to create a thread.
      2. Step 2: Match with code snippet

        The correct assignment is predecessor.right = current; to create the temporary link.
      3. Final Answer:

        predecessor.right = current; -> Option C
      4. Quick Check:

        Thread link = predecessor.right = current [OK]
      Hint: Thread link always sets predecessor.right to current [OK]
      Common Mistakes:
      • Assigning current.left to predecessor instead
      • Using predecessor.left instead of right
      • Confusing current.right with predecessor.right
      3. What is the output of the Morris inorder traversal on the following binary tree?
          2
         / \
        1   3
      
      medium
      A. [1, 2, 3]
      B. [2, 1, 3]
      C. [3, 2, 1]
      D. [1, 3, 2]

      Solution

      1. Step 1: Perform inorder traversal manually

        Inorder traversal visits left subtree, root, then right subtree. For this tree: left=1, root=2, right=3.
      2. Step 2: List nodes in inorder

        Nodes visited in order: 1, 2, 3.
      3. Final Answer:

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

        Inorder = left-root-right = [1, 2, 3] [OK]
      Hint: Inorder = left, root, right sequence [OK]
      Common Mistakes:
      • Confusing preorder or postorder with inorder
      • Mixing up left and right child order
      • Assuming Morris changes traversal order
      4. Consider this buggy snippet from a Morris inorder traversal implementation:
      while (current != null) {
        if (current.left == null) {
          System.out.print(current.val + " ");
          current = current.right;
        } else {
          TreeNode predecessor = current.left;
          while (predecessor.right != null) {
            predecessor = predecessor.right;
          }
          predecessor.right = current;
          current = current.left;
        }
      }
      What is the main bug causing an infinite loop?
      medium
      A. Not printing current.val when left is not null
      B. Not checking if predecessor.right == current before linking
      C. Not moving current to current.right when left is null
      D. Not initializing predecessor correctly

      Solution

      1. Step 1: Identify missing condition in inner loop

        The inner while loop should stop if predecessor.right == current to avoid infinite loop.
      2. Step 2: Explain infinite loop cause

        Without checking predecessor.right == current, the loop never breaks and keeps moving right, causing infinite loop.
      3. Final Answer:

        Not checking if predecessor.right == current before linking -> Option B
      4. Quick Check:

        Check predecessor.right == current to break loop [OK]
      Hint: Always check predecessor.right == current to avoid loops [OK]
      Common Mistakes:
      • Ignoring thread existence check
      • Assuming printing happens in else block
      • Incorrect predecessor initialization
      5. Given a binary tree where some nodes have only left children, how does Morris Traversal handle these nodes to produce the inorder sequence without extra space?
      hard
      A. It creates temporary right links from the rightmost node of left subtree back to current node
      B. It skips nodes with only left children to avoid infinite loops
      C. It uses recursion internally to handle left-only nodes
      D. It converts the tree into a linked list before traversal

      Solution

      1. Step 1: Understand Morris Traversal threading

        For nodes with left children, Morris Traversal finds the rightmost node in left subtree and links its right pointer to current node temporarily.
      2. Step 2: Explain handling of left-only nodes

        This threading allows traversal to return to current node after left subtree is processed, enabling inorder traversal without stack or recursion.
      3. Final Answer:

        It creates temporary right links from the rightmost node of left subtree back to current node -> Option A
      4. Quick Check:

        Threading rightmost left subtree node to current [OK]
      Hint: Thread rightmost left child back to current node [OK]
      Common Mistakes:
      • Thinking recursion is used internally
      • Assuming nodes are skipped
      • Believing tree is converted to linked list
      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