Bird
Raised Fist0
DSA C#programming~5 mins

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

Choose your learning style10 modes available
Recall & Review
beginner
What is the main advantage of Morris Traversal for inorder tree traversal?
Morris Traversal allows inorder traversal of a binary tree without using extra space for stack or recursion by temporarily modifying the tree's structure.
Click to reveal answer
intermediate
In Morris Traversal, what is the purpose of creating a temporary thread (link) from the rightmost node of the left subtree to the current node?
The temporary thread helps to return to the current node after finishing the left subtree traversal without using a stack or recursion.
Click to reveal answer
intermediate
What condition indicates that the left subtree of the current node has been fully visited in Morris Traversal?
When the temporary thread from the rightmost node of the left subtree points back to the current node, it means the left subtree is fully visited.
Click to reveal answer
beginner
Show the basic steps of Morris Traversal inorder algorithm.
1. Initialize current as root. 2. While current is not null: a. If current has no left child, print current and move to right child. b. Else, find rightmost node in left subtree. i. If rightmost node's right is null, set it to current (thread) and move current to left child. ii. Else, revert the thread, print current, and move current to right child.
Click to reveal answer
beginner
Why does Morris Traversal restore the tree structure after traversal?
Because Morris Traversal temporarily modifies the tree by creating threads, it restores the original tree structure to avoid side effects after traversal.
Click to reveal answer
What data structure does Morris Traversal avoid using for inorder traversal?
AHash Table
BStack
CQueue
DLinked List
In Morris Traversal, what do we do when the current node has no left child?
AMove to left child
BCreate a thread to the current node
CPrint current node and move to right child
DStop traversal
How does Morris Traversal find the rightmost node of the left subtree?
ABy moving right until right child is null or points to current node
BBy moving left until left child is null
CBy using a stack to track nodes
DBy recursion
What happens when the rightmost node's right pointer points back to the current node?
ALeft subtree is skipped
BTraversal ends
CA new thread is created
DThe thread is removed, current node is printed, and traversal moves to right child
What is the space complexity of Morris Traversal?
AO(1)
BO(n)
CO(log n)
DO(n log n)
Explain how Morris Traversal performs inorder traversal without using a stack or recursion.
Think about how the traversal remembers where to return after left subtree.
You got /3 concepts.
    Describe the steps to find the rightmost node in the left subtree during Morris Traversal and how it helps in traversal.
    Focus on how the traversal uses this node to link back to current.
    You got /3 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 works only on binary search trees.
      B. It is faster than all other traversal methods.
      C. It uses no extra space for stack or recursion.
      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

        Recursive and stack-based traversals use O(h) space where h is tree height, so Morris Traversal saves memory.
      3. Final Answer:

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

        Space complexity = O(1) [OK]
      Hint: Morris Traversal uses threading to avoid extra space [OK]
      Common Mistakes:
      • Thinking it is faster than recursive traversal
      • Assuming it only works on BSTs
      • Believing it permanently changes the tree
      2. Which of the following C# code snippets correctly initializes the current node for Morris inorder traversal?
      easy
      A. TreeNode current = root;
      B. TreeNode current = null;
      C. TreeNode current = root.left;
      D. TreeNode 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 correctness

        Initializing current as root ensures traversal begins at the tree's root.
      3. Final Answer:

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

        Start traversal at root node [OK]
      Hint: Start traversal from root node always [OK]
      Common Mistakes:
      • Starting from null causes no traversal
      • Starting from left or right child misses nodes
      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 separated by spaces.
      medium
      A. 3 2 1
      B. 1 2 3
      C. 2 1 3
      D. 1 3 2

      Solution

      1. Step 1: Understand inorder traversal order

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

        Left child 1, then root 2, then right child 3 results in "1 2 3".
      3. Final Answer:

        1 2 3 -> Option B
      4. Quick Check:

        Inorder = left-root-right = 1 2 3 [OK]
      Hint: Inorder = left, root, right [OK]
      Common Mistakes:
      • Confusing preorder or postorder with inorder
      • Misreading tree structure
      4. Consider this snippet of Morris inorder traversal code in C#:
      
      while (current != null) {
          if (current.left == null) {
              Console.Write(current.val + " ");
              current = current.right;
          } else {
              TreeNode predecessor = current.left;
              while (predecessor.right != null && predecessor.right == current) {
                  predecessor = predecessor.right;
              }
              if (predecessor.right == null) {
                  predecessor.right = current;
                  current = current.left;
              } else {
                  predecessor.right = null;
                  Console.Write(current.val + " ");
                  current = current.right;
              }
          }
      }
      
      What is the bug in this code?
      medium
      A. The code does not handle the case when current.left is null.
      B. The Console.Write statements should be outside the if-else blocks.
      C. The predecessor.right pointer is never reset to null, causing infinite loop.
      D. The inner while loop should check predecessor.right != current instead of predecessor.right == current.

      Solution

      1. Step 1: Analyze inner while loop condition

        The loop should continue while predecessor.right != null && predecessor.right != current to reach the rightmost node in left subtree.
      2. Step 2: Check given condition correctness

        The code uses predecessor.right == current, which fails to traverse the right chain properly, as it only continues if already threaded back.
      3. Step 3: Why other options are wrong

        A: left null is handled in if. C: reset happens in else. D: print placement is correct for inorder.
      4. Final Answer:

        The inner while loop should check predecessor.right != current instead of predecessor.right == current. -> Option D
      5. Quick Check:

        Loop condition must be != null && != current [OK]
      Hint: Check loop conditions carefully to avoid infinite loops [OK]
      Common Mistakes:
      • Confusing loop condition signs
      • Forgetting to reset threaded pointers
      • Misplacing print statements
      5. You want to modify Morris inorder traversal to count the number of nodes in a binary tree without using extra space or recursion. Which modification is correct?
      hard
      A. Increment a counter each time you print a node's value during traversal.
      B. Increment a counter only when you create a threaded link.
      C. Increment a counter only when you remove a threaded link.
      D. Increment a counter only when current.left is null.

      Solution

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

        Nodes are visited (printed) when current.left is null or after removing threaded link.
      2. Step 2: Counting nodes correctly

        Incrementing counter when printing nodes ensures counting all nodes exactly once.
      3. Step 3: Why other options fail

        Incrementing only on creating or removing threaded links misses nodes or counts duplicates.
      4. Final Answer:

        Increment a counter each time you print a node's value during traversal. -> Option A
      5. Quick Check:

        Count nodes when visiting them [OK]
      Hint: Count nodes when printing during traversal [OK]
      Common Mistakes:
      • Counting on threaded link creation/removal
      • Counting only left-null nodes
      • Missing some nodes in count
      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