Bird
Raised Fist0
DSA Goprogramming~5 mins

Morris Traversal Inorder Without Stack in DSA Go - 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 uses no extra space like stack or recursion. It modifies the tree temporarily to achieve inorder traversal with O(1) space.
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?
This 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 happens when the temporary thread in Morris Traversal is encountered again during traversal?
When the thread is encountered again, it means the left subtree is fully visited. The thread is removed, and the current node is processed (printed). Then traversal moves to the right subtree.
Click to reveal answer
beginner
Show the basic steps of Morris Traversal inorder algorithm.
  1. Initialize current as root.
  2. If current has no left child, print current and move to right child.
  3. If current has left child, find rightmost node in left subtree.
  4. If rightmost node's right is null, make it point to current (thread) and move current to left child.
  5. If rightmost node's right points to current, remove thread, print current, move to right child.
Click to reveal answer
beginner
Why is Morris Traversal considered efficient in terms of space compared to recursive or stack-based inorder traversal?
Because it does not use extra memory for stack or recursion call stack. It only uses the tree's existing pointers temporarily, achieving O(1) space complexity.
Click to reveal answer
What data structure does Morris Traversal avoid using for inorder traversal?
AHash Map
BQueue
CStack
DLinked List
In Morris Traversal, what is the temporary link called that connects the rightmost node of the left subtree to the current node?
AThread
BPointer
CEdge
DBridge
What is the time complexity of Morris Traversal inorder traversal?
AO(n)
BO(n log n)
CO(n^2)
DO(log n)
What happens to the temporary thread after the left subtree is fully traversed in Morris Traversal?
AIt remains permanently
BIt points to the root
CIt is converted to a left child
DIt is removed
Which traversal order does Morris Traversal implement without stack or recursion?
APostorder
BInorder
CPreorder
DLevel order
Explain how Morris Traversal achieves inorder traversal without using a stack or recursion.
Think about how the traversal remembers where to return after left subtree.
You got /4 concepts.
    Describe the steps to find the rightmost node in the left subtree during Morris Traversal and how it is used.
    Focus on how the rightmost node helps in creating and removing threads.
    You got /4 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 is faster than all other traversal methods
      B. It requires the tree to be balanced
      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 the tree temporarily to avoid using stack or recursion, 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 D
      4. Quick Check:

        Morris Traversal = O(1) space [OK]
      Hint: Morris Traversal saves space by threading nodes [OK]
      Common Mistakes:
      • Thinking it is faster than all methods
      • Assuming it only works on BSTs
      • Believing it needs balanced trees
      2. Which of the following Go code snippets correctly initializes a pointer to the current node for Morris inorder traversal?
      easy
      A. current := root
      B. var current *Node = nil
      C. current := &root
      D. current := root.left

      Solution

      1. Step 1: Understand pointer initialization

        For traversal, current should start at root node, so current := root is correct.
      2. Step 2: Check other options

        var current *Node = nil sets current to nil, which stops traversal immediately. current := &root takes address of root pointer, incorrect. current := root.left starts at left child, skipping root.
      3. Final Answer:

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

        Start traversal at root = current := root [OK]
      Hint: Start traversal pointer at root node [OK]
      Common Mistakes:
      • Initializing current as nil
      • Taking address of root pointer
      • Starting at root.left instead of root
      3. Given the following binary tree and Morris inorder traversal code, what is the printed output?
          2
         / \
        1   3
      
      func morrisInorder(root *Node) {
          current := root
          for current != nil {
              if current.Left == nil {
                  fmt.Print(current.Val, " ")
                  current = current.Right
              } else {
                  pre := current.Left
                  for pre.Right != nil && pre.Right != current {
                      pre = pre.Right
                  }
                  if pre.Right == nil {
                      pre.Right = current
                      current = current.Left
                  } else {
                      pre.Right = nil
                      fmt.Print(current.Val, " ")
                      current = current.Right
                  }
              }
          }
      }
      medium
      A. 2 1 3
      B. 1 2 3
      C. 3 2 1
      D. 1 3 2

      Solution

      1. Step 1: Understand inorder traversal order

        Inorder traversal visits left subtree, root, then right subtree. For tree 2 with left 1 and right 3, order is 1 2 3.
      2. Step 2: Trace Morris traversal output

        Morris traversal prints nodes in inorder without stack. The code prints 1, then 2, then 3 with spaces.
      3. Final Answer:

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

        Inorder of given tree = 1 2 3 [OK]
      Hint: Inorder prints left-root-right sequence [OK]
      Common Mistakes:
      • Confusing preorder or postorder output
      • Misreading the tree structure
      • Ignoring Morris threading logic
      4. Consider this snippet of Morris inorder traversal in Go. What is the bug that causes an infinite loop?
      current := root
      for current != nil {
          if current.Left != nil {
              pre := current.Left
              for pre.Right != nil {
                  pre = pre.Right
              }
              pre.Right = current
              current = current.Left
          } else {
              fmt.Print(current.Val, " ")
              current = current.Right
          }
      }
      medium
      A. Printing node values before traversal
      B. Incorrect initialization of current pointer
      C. Missing check if pre.Right == current before setting pre.Right = current
      D. Not updating current to current.Left in else block

      Solution

      1. Step 1: Analyze inner loop for threading

        The inner loop finds rightmost node of left subtree. Without checking if pre.Right == current, it keeps looping infinitely.
      2. Step 2: Identify missing condition causing infinite loop

        Correct Morris traversal checks if pre.Right is nil or points to current to avoid infinite loop. Missing this causes infinite loop.
      3. Final Answer:

        Missing check if pre.Right == current before setting pre.Right = current -> Option C
      4. Quick Check:

        Check pre.Right == current to avoid loops [OK]
      Hint: Always check if thread already exists before linking [OK]
      Common Mistakes:
      • Skipping pre.Right == current check
      • Wrong pointer updates
      • Printing too early
      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 change correctly achieves this?
      hard
      A. Add a counter variable and increment it each time you print a node's value during traversal
      B. Increment counter only when creating a thread (pre.Right = current)
      C. Increment counter only when removing a thread (pre.Right = nil)
      D. Increment counter only when current.Left is nil

      Solution

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

        Nodes are printed exactly once: when current.Left == nil or when removing the thread (pre.Right != nil).
      2. Step 2: Choose correct increment point for counting

        Incrementing the counter each time a node's value is printed ensures every node is counted exactly once.
      3. Final Answer:

        Add a counter variable and increment it each time you print a node's value during traversal -> Option A
      4. Quick Check:

        Increment on every print operation [OK]
      Hint: Count nodes when printing each node [OK]
      Common Mistakes:
      • Incrementing only when current.Left is nil (misses nodes with left subtrees)
      • Incrementing when creating threads (counts only non-leaf nodes)
      • Incrementing when removing threads (counts only non-leaf nodes)
      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