Unit 9 Session 1 (Click for link to problem statements)
Understand what the interviewer is asking for by using test cases and questions about the problem.
order_tree
is None
?
None
since there are no orders to process.order
is the last node on its level?
None
since there are no more orders on the same level.order
node be assumed to exist in the tree?
order
is a valid node in the tree.HAPPY CASE
Input:
cupcakes = TreeNode("Cupcakes")
macaron = TreeNode("Macaron")
cookies = TreeNode("Cookies")
cake = TreeNode("Cake")
eclair = TreeNode("Eclair")
croissant = TreeNode("Croissant")
cupcakes.left, cupcakes.right = macaron, cookies
macaron.right = cake
cookies.left, cookies.right = eclair, croissant
next_order1 = find_next_order(cupcakes, cake)
next_order2 = find_next_order(cupcakes, cookies)
Output: Eclair, None
Explanation:
* The next order after Cake on the same level is Eclair.
* Cookies is the last order on its level, so return None.
EDGE CASE
Input: order_tree = None, order = None
Output: None
Explanation: The tree is empty, so return None.
Input: order_tree = TreeNode("Cupcakes"), order = TreeNode("Cupcakes")
Output: None
Explanation: There is only one order in the tree, so return None.
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 problems involving finding the next node on the same level in a binary tree, we can consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
1) Initialize:
order_tree
is empty (None
), return None
.level_size
).order
.None
.None
if the target order
is not found in the tree.Pseudocode:
1) If `order_tree` is `None`, return `None`.
2) Initialize a queue with `order_tree` as the first element.
3) While the queue is not empty:
a) Determine the number of nodes at the current level (`level_size = len(queue)`).
b) For each node in the current level:
i) Dequeue the node.
ii) If the node matches `order`, check if it's the last node on the level:
* If so, return `None`.
* Otherwise, return the next node in the queue.
iii) If the node has a left child, enqueue it.
iv) If the node has a right child, enqueue it.
4) Return `None` if the target `order` is not found in the tree.
Implement the code to solve the algorithm.
from collections import deque
class TreeNode:
def __init__(self, order, left=None, right=None):
self.val = order
self.left = left
self.right = right
def find_next_order(order_tree, order):
if not order_tree:
return None
queue = deque([order_tree])
while queue:
level_size = len(queue)
for i in range(level_size):
node = queue.popleft()
# Check if this is the node we are currently fulfilling
if node == order:
# If it's the last node on the level, return None
if i == level_size - 1:
return None
else:
# The next node in the queue is the next order on the same level
return queue.popleft()
# Add children to the queue
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return None # If order is not found in the tree
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
order_tree = TreeNode("Cupcakes"), order = TreeNode("Cake")
:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in the tree.
O(N)
because each node in the tree may need to be visited to find the target node.O(N)
due to the queue storing nodes at each level during traversal.