Bird
Raised Fist0
DSA Pythonprogramming~5 mins

Morris Traversal Inorder Without Stack in DSA Python - 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). If it already points to the current node, we remove the thread and move to the right child.
Click to reveal answer
beginner
Why does Morris Traversal not require a stack or recursion?
Because it temporarily changes the tree structure by creating threads to predecessors, it can return to nodes after visiting left subtrees 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 step when Morris Traversal visits a node with no left child.
If the current node has no left child, we print its value and move directly to its right child.
Click to reveal answer
What does Morris Traversal use to avoid stack or recursion?
AExtra stack memory
BTemporary threads to predecessor nodes
CRecursion with memoization
DQueue data structure
When the predecessor's right pointer is null, what does Morris Traversal do?
ADeletes the predecessor node
BMoves to the right child immediately
CSets it to point to the current node
DEnds the traversal
What happens when the predecessor's right pointer already points to the current node?
AThe thread is removed and current node is visited
BTraversal restarts from root
CThe left subtree is skipped
DAn error occurs
What is the space complexity of Morris Traversal?
AO(1)
BO(n)
CO(log n)
DO(n log n)
Which traversal order does Morris Traversal implement in this context?
ALevel order
BPreorder
CPostorder
DInorder
Explain step-by-step how Morris Traversal performs inorder traversal without using a stack.
Think about how the traversal remembers where to return without extra memory.
You got /5 concepts.
    Describe the advantages and possible drawbacks of using Morris Traversal for inorder traversal.
    Consider memory use and tree modification.
    You got /5 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 uses no extra space for stack or recursion.
      B. It is faster than all other traversal methods.
      C. It only works on binary search trees.
      D. It modifies the tree permanently.

      Solution

      1. Step 1: Understand Morris Traversal space usage

        Morris Traversal uses threaded binary trees to avoid recursion or stack, so it uses O(1) extra space.
      2. Step 2: Compare with other methods

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

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

        Space complexity = O(1) [OK]
      Hint: Morris uses threads, no stack or recursion needed [OK]
      Common Mistakes:
      • Thinking Morris is faster in time complexity
      • Believing it only works on BSTs
      • Assuming it permanently changes the tree
      2. Which of the following code snippets correctly initializes the current node for Morris inorder traversal in Python?
      easy
      A. current = root
      B. current = None
      C. current = root.left
      D. current = root.right

      Solution

      1. Step 1: Identify starting point for traversal

        Morris traversal starts from the root node to cover the entire tree.
      2. Step 2: Check initialization options

        Setting current = root ensures traversal begins at the tree's root.
      3. Final Answer:

        current = root -> Option A
      4. Quick Check:

        Start at root node [OK]
      Hint: Always start traversal from root node [OK]
      Common Mistakes:
      • Starting from None causes no traversal
      • Starting from root.left misses root node
      • Starting from root.right misses left subtree
      3. What will be the output of the Morris inorder traversal on the following binary tree?
      
          2
         / \
        1   3
      
      Assuming the traversal prints node values in order.
      medium
      A. [2, 1, 3]
      B. [1, 3, 2]
      C. [3, 2, 1]
      D. [1, 2, 3]

      Solution

      1. Step 1: Understand inorder traversal order

        Inorder traversal visits left subtree, root, then right subtree.
      2. Step 2: Apply Morris traversal order

        For the tree, left is 1, root is 2, right is 3, so output is [1, 2, 3].
      3. Final Answer:

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

        Inorder = left-root-right = [1, 2, 3] [OK]
      Hint: Inorder = left, root, right always [OK]
      Common Mistakes:
      • Confusing preorder or postorder with inorder
      • Misreading tree structure
      • Assuming traversal prints in insertion order
      4. Consider this snippet of Morris inorder traversal code in Python:
      
      current = root
      while current:
          if current.left is None:
              print(current.val)
              current = current.right
          else:
              pre = current.left
              while pre.right and pre.right != current:
                  pre = pre.right
              if pre.right is None:
                  pre.right = current
                  current = current.left
              else:
                  pre.right = None
                  print(current.val)
                  current = current.right
      
      What is the main bug if the line pre.right = None is missing?
      medium
      A. The traversal will print nodes in preorder instead of inorder.
      B. The traversal will skip the root node.
      C. The traversal will enter an infinite loop.
      D. The traversal will cause a syntax error.

      Solution

      1. Step 1: Understand role of pre.right = None

        This line removes the temporary thread created to return to the root after left subtree traversal.
      2. Step 2: Consequence of missing this line

        If not removed, the thread remains, causing the loop to revisit nodes endlessly, creating an infinite loop.
      3. Final Answer:

        The traversal will enter an infinite loop. -> Option C
      4. Quick Check:

        Missing thread removal = infinite loop [OK]
      Hint: Always remove temporary threads to avoid loops [OK]
      Common Mistakes:
      • Thinking it causes syntax error
      • Assuming it changes traversal order
      • Believing it skips nodes
      5. You want to modify Morris inorder traversal to collect node values in a list instead of printing them. Which of the following code changes correctly achieves this without changing the traversal logic?
      
      result = []
      current = root
      while current:
          if current.left is None:
              # Change here
              current = current.right
          else:
              pre = current.left
              while pre.right and pre.right != current:
                  pre = pre.right
              if pre.right is None:
                  pre.right = current
                  current = current.left
              else:
                  pre.right = None
                  # Change here
                  current = current.right
      
      hard
      A. Replace only second '# Change here' line with result.append(current.val)
      B. Replace both '# Change here' lines with result.append(current.val)
      C. Replace only first '# Change here' line with result.append(current.val)
      D. Replace both '# Change here' lines with print(current.val)

      Solution

      1. Step 1: Identify where nodes are visited

        In Morris traversal, nodes are visited and printed in two places: when left is None and after removing thread.
      2. Step 2: Replace print with appending to list

        To collect values, replace both print statements with result.append(current.val) to capture all visited nodes.
      3. Final Answer:

        Replace both '# Change here' lines with result.append(current.val) -> Option B
      4. Quick Check:

        Collect all visited nodes in list [OK]
      Hint: Append values at both visit points to collect all nodes [OK]
      Common Mistakes:
      • Appending only once misses some nodes
      • Leaving print statements causes unwanted output
      • Changing traversal logic breaks correctness
      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