Practice
inorderTraversal on this tree?Tree structure:
2
/ \ 1 3
from typing import Optional, List
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def inorderTraversal(root: Optional[TreeNode]) -> List[int]:
result = []
current = root
while current:
if not current.left:
result.append(current.val) # visit node
current = current.right
else:
predecessor = current.left
while predecessor.right and predecessor.right != current:
predecessor = predecessor.right
if not predecessor.right:
predecessor.right = current # create thread
current = current.left
else:
predecessor.right = None # remove thread
result.append(current.val) # visit node
current = current.right
return result
Solution
Step 1: Trace traversal starting at root=2
Current=2 has left child 1, find predecessor in left subtree: node 1 (no right child). Create thread from 1.right to 2, move current to 1.Step 2: Visit node 1 (no left child), append 1, move current to 1.right which points to 2 (thread).
Remove thread, append 2, move current to 2.right=3. Node 3 has no left child, append 3, move current to null.Final Answer:
Option A -> Option AQuick Check:
Inorder traversal of tree 1,2,3 is [1,2,3] [OK]
- Appending before removing thread
- Visiting root before left subtree
- Confusing preorder with inorder output
def postorderTraversal(root):
result = []
stack = []
last_visited = None
current = root
while current or stack:
while current:
stack.append(current)
current = current.left
peek_node = stack[-1]
if peek_node.right and last_visited != peek_node.right:
current = peek_node.right
else:
result.append(peek_node.val)
last_visited = stack.pop()
return result
Solution
Step 1: Trace traversal on given tree
Start at root(1), go left (None), push 1. Then peek 1, right child is 2, move to 2, push 2, go left to 3, push 3, left None.Step 2: Process nodes in postorder
3 has no children, append 3. Back to 2, right visited, append 2. Back to 1, right visited, append 1.Final Answer:
Option C -> Option CQuick Check:
Output matches postorder [3, 2, 1] [OK]
- Confusing order of appending nodes
- Off-by-one in stack popping
Solution
Step 1: Understand the problem constraints
Reconstructing a tree from inorder and postorder requires identifying root nodes and splitting subtrees efficiently.Step 2: Evaluate approaches
Recursion with a hash map allows O(1) root index lookup in inorder, avoiding repeated linear searches and ensuring O(n) time.Final Answer:
Option C -> Option CQuick Check:
Hash map lookup avoids O(n) search per recursion [OK]
- Assuming greedy or BFS can reconstruct tree uniquely
- Confusing DP with tree construction
- Ignoring index lookup cost
def preorderTraversal(root):
result = []
current = root
while current:
if current.left is None:
result.append(current.val)
current = current.right
else:
predecessor = current.left
while predecessor.right and predecessor.right != current:
predecessor = predecessor.right
if predecessor.right is None:
predecessor.right = current
result.append(current.val)
current = current.left
else:
# Bug here
current = current.right
return result
Solution
Step 1: Identify missing restoration of tree structure
In Morris traversal, after visiting left subtree, predecessor.right must be reset to None to restore original tree.Step 2: Locate missing line
The else block lacks the linepredecessor.right = None, causing the threaded link to persist.Final Answer:
Option A -> Option AQuick Check:
Without resetting, tree remains modified after traversal [OK]
- Forgetting to restore tree structure
- Appending nodes in wrong order
- Incorrect loop conditions
Solution
Step 1: Understand the impact of allowing node reuse
Allowing cycles means paths can revisit nodes infinitely, so naive DFS or postorder traversal fails due to infinite loops.Step 2: Identify correct approach
Dynamic programming with cycle detection and memoization prevents infinite recursion and correctly computes max sums with repeated nodes.Final Answer:
Option B -> Option BQuick Check:
Memoization with cycle detection handles repeated nodes safely [OK]
- Using same postorder traversal causes infinite loops
- Bellman-Ford is for shortest paths, not max sums with negative cycles
- DFS without cycle checks leads to infinite recursion
