TIP102 Unit 9 Session 2 Advanced (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
True
if the order is complete and False
otherwise.HAPPY CASE
Input:
items = ["Croissant", "Cupcake", "Bagel", "Cake", "Pie", "Blondies"]
Output:
True
Explanation:
The tree structure:
Croissant
/ \
Cupcake Bagel
/ \ /
Cake Pie Blondies
The order is complete as all levels, except possibly the last, are completely filled, and the last level items are as far left as possible.
EDGE CASE
Input:
items = ["Croissant", "Cupcake", "Bagel", "Cake", "Pie", None, "Blondies"]
Output:
False
Explanation:
The tree structure:
Croissant
/ \
Cupcake Bagel
/ \ \
Cake Pie Blondies
The order is not complete because there is a gap in the last level (right child of Bagel without a corresponding left child).
Match what this problem looks like to known categories of problems, e.g., Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Tree Completeness problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea:
None
node is encountered, mark that any subsequent nodes in the level order must also be None
to maintain completeness.None
, the tree is not complete.1) Initialize a queue and add the root node to it.
2) Use a variable `encountered_none` to track if a `None` node has been encountered.
3) Perform a BFS traversal:
- Dequeue the next node.
- If the node is not `None`, check if `encountered_none` is `True`. If it is, return `False`.
- Otherwise, add the node's left and right children to the queue (even if they are `None`).
- If the node is `None`, set `encountered_none` to `True`.
4) If the BFS completes without finding any issues, return `True`.
⚠️ Common Mistakes
None
nodes appearing in between valid nodes.None
nodes when traversing through the tree.Implement the code to solve the algorithm.
from collections import deque
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def is_complete(root):
if not root:
return True
queue = deque([root])
encountered_none = False
while queue:
node = queue.popleft()
if node:
if encountered_none:
# If we previously encountered a None, and now we see a node, it's not complete
return False
queue.append(node.left)
queue.append(node.right)
else:
encountered_none = True
return True
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Example 1:
- Input:
`items = ["Croissant", "Cupcake", "Bagel", "Cake", "Pie", "Blondies"]`
- Execution:
- Traverse the tree using BFS, ensuring all levels are complete.
- Output:
True
- Example 2:
- Input:
`items = ["Croissant", "Cupcake", "Bagel", "Cake", "Pie", None, "Blondies"]`
- Execution:
- Traverse the tree using BFS, detect the gap in the last level.
- Output:
False
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
O(N)
where N
is the number of nodes in the tree.
O(W)
where W
is the maximum width of the tree.
O(N/2)
in a balanced tree.O(N)
where N
is the number of nodes.