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
preorderTraversal on the tree below?
Tree structure:
1
/ \
2 3
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:
predecessor.right = None
current = current.right
return result
Solution
Step 1: Trace first iteration with current=1
Node 1 has left child 2, predecessor is 2 with no right child, set 2.right=1, append 1, move current=2.Step 2: Trace second iteration with current=2
Node 2 has no left child, append 2, move current=2.right which points back to 1 (thread).Step 3: Detect thread at 2.right=1
Since predecessor.right == current, reset 2.right=None, move current=1.right=3.Step 4: Trace current=3
Node 3 has no left child, append 3, move current=3.right=None, loop ends.Final Answer:
Option B -> Option BQuick Check:
Output matches preorder traversal [1,2,3] [OK]
- Appending nodes in wrong order
- Not resetting threaded links
- Confusing left and right child traversal
Solution
Step 1: Identify camera placement logic
When a child is NOT_COVERED, a camera must be placed at current node and dfs must return HAS_CAM to indicate camera presence.Step 2: Locate incorrect return
The code returns COVERED_NO_CAM after placing a camera, which falsely signals no camera here, causing parents to misinterpret coverage.Final Answer:
Option A -> Option AQuick Check:
Returning HAS_CAM is essential after placing a camera [OK]
- Returning COVERED_NO_CAM instead of HAS_CAM after camera placement
- Forgetting to add camera if root uncovered
- Mixing coverage states in conditions
Solution
Step 1: Analyze node visits
Each node is pushed and popped exactly once, so operations proportional to n.Step 2: Confirm no nested repeated work
Inner loops traverse down left children, but total iterations over all nodes sum to n.Final Answer:
Option D -> Option DQuick Check:
Linear time complexity matches traversal of all nodes once [OK]
- Assuming nested loops multiply to n^2
- Confusing auxiliary space with time
Solution
Step 1: Understand the problem change
The tree is no longer complete, so properties used by optimal methods do not hold.Step 2: Identify correct counting method
Only a full traversal (DFS or BFS) guarantees counting all nodes correctly in O(n) time.Final Answer:
Option D -> Option DQuick Check:
Optimal methods rely on completeness, which is lost here [OK]
- Trying to apply binary search on last level
- Assuming height checks still reduce complexity
