Practice
Solution
Step 1: Perform inorder traversal on BST with nodes 1, 2, 3
Inorder traversal yields nodes in sorted order: [1, 2, 3].Step 2: Iteratively build balanced BST choosing middle node
Middle index is 1 (0-based), so node with value 2 becomes root.Final Answer:
Option B -> Option BQuick Check:
Middle of [1,2,3] is 2 -> root value 2 [OK]
- Choosing left or right middle always, skewing tree
- Confusing preorder with inorder traversal results
- Forgetting to reset left/right pointers causing cycles
helper with input nums = [1, 3, 5]?Solution
Step 1: Calculate mid index for initial call
For nums=[1,3,5], left=0, right=2, mid=(0+2)//2=1.Step 2: Identify root value
nums[mid] = nums[1] = 3, so root node value is 3.Final Answer:
Option A -> Option AQuick Check:
Mid index calculation picks 3 as root [OK]
- Choosing first or last element as root
- Off-by-one mid calculation
Solution
Step 1: Understand BST property and inorder traversal
Inorder traversal of a BST produces a sorted array of node values.Step 2: Apply two-pointer technique on sorted array
Using two pointers at start and end, we can efficiently find if two values sum to k in O(n) time and O(n) space.Final Answer:
Option A -> Option AQuick Check:
Inorder + two pointers is optimal and uses BST ordering [OK]
- Trying nested loops directly on BST nodes without flattening
- Assuming greedy approach works without sorting
- Using DP which is unnecessary here
Solution
Step 1: Trace first tree (2,1,3)
Inorder traversal yields [1,2,3], strictly increasing, so returns true.Step 2: Trace second tree (5,1,4 with children 3 and 6)
Inorder traversal yields [1,5,3,4,6], 3 < 5 violates BST property, so returns false.Final Answer:
Option A -> Option AQuick Check:
First tree valid BST, second invalid due to subtree violation [OK]
- Assuming subtree root comparison is enough
- Confusing output order
- Missing strict inequality check
Solution
Step 1: Understand duplicates in BST
Duplicates can be on left or right subtree depending on implementation; inclusive range means nodes equal to low or high must be kept.Step 2: Modify pruning conditions
Use <= and >= in comparisons to keep nodes equal to boundaries, ensuring duplicates within range remain.Step 3: Avoid rebuilding
Rebuilding wastes time and space; pruning with adjusted conditions preserves BST and duplicates correctly.Final Answer:
Option C -> Option CQuick Check:
Inclusive pruning keeps duplicates at boundaries [OK]
- Using strict inequalities excludes boundary duplicates
- Rebuilding unnecessarily wastes resources
