Practice
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
Solution
Step 1: Identify handling of empty tree
The code does not check if root is None before starting traversal, which can cause errors or incorrect results.Step 2: Verify other lines
Other lines handle null children safely using heights.get with default 0, and last_visited logic is correct.Final Answer:
Option A -> Option AQuick Check:
Missing early return for empty root causes bug [OK]
- Forgetting to handle empty tree as balanced
- Incorrectly comparing last_visited causing infinite loops
- Not defaulting heights for null children
Solution
Step 1: Analyze the height function calls.
Height is called for each node, and each call traverses its subtree, leading to repeated traversals.Step 2: Calculate total complexity.
For each of the n nodes, height is computed which can take O(n) in worst case, resulting in O(n^2) total time.Final Answer:
Option C -> Option CQuick Check:
Repeated height computations cause quadratic time [OK]
- Assuming height calls are O(1)
- Confusing recursion stack space with time
- Thinking it's O(n log n) due to balanced tree
Solution
Step 1: Understand traversal constraints
Without left/right child pointers, Morris traversal is not applicable since it relies on child links.Step 2: Use parent pointers to simulate traversal
By tracking current and previously visited nodes, we can move up or down the tree to simulate inorder traversal iteratively.Final Answer:
Option D -> Option DQuick Check:
Tracking previous node enables correct traversal without extra space [OK]
- Trying to create threads on parent pointers
- Using recursion without child pointers
- Confusing BFS with inorder traversal
Solution
Step 1: Understand reuse requirement
Allowing nodes to appear multiple times means the original nodes cannot be simply rewired in-place without duplication.Step 2: Identify necessary modification
Cloning nodes during traversal is required to create multiple instances, preserving original tree structure and allowing duplicates.Step 3: Why other options fail
No change needed; the current in-place reverse preorder traversal works as is. fails because in-place rewiring destroys original nodes. Use a preorder traversal to collect nodes in a list and rebuild the linked list allowing duplicates. collects nodes but does not clone them. Switch to a postorder traversal to ensure all duplicates are appended after processing children. traversal order does not address duplication.Final Answer:
Option C -> Option CQuick Check:
Cloning nodes enables multiple appearances in flattened list [OK]
- Assuming in-place rewiring supports duplicates
- Collecting nodes without cloning leads to lost references
- Changing traversal order alone does not solve duplication
