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.
- 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?
What should be returned if the tree is empty?
How should division by zero be handled?
HAPPY CASE
* (root)
/ \
+ *
/ \ / \
3 2 4 5
Input: root
Output: 50
Explanation: The evaluation proceeds as follows:
- Evaluate left subtree [+ 3 2] -> 3 + 2 = 5
- Evaluate right subtree [* 4 5] -> 4 * 5 = 20
- Apply root operation: 5 * 20 = 100
EDGE CASE
+ (root)
/ \
- *
/ \ / \
10 5 2 3
Input: root
Output: 11
Explanation: The evaluation proceeds as follows:
- Evaluate left subtree [- 10 5] -> 10 - 5 = 5
- Evaluate right subtree [* 2 3] -> 2 * 3 = 6
- Apply root operation: 5 + 6 = 11
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 problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a recursive approach to evaluate the tree. For each node, if it is a leaf node, return its value. Otherwise, evaluate its left and right children and apply the node's operation.
1) Base case: If the node is a leaf (no children), return its integer value.
2) Recursive case: Evaluate the left and right children.
3) Apply the operation of the current node to the results of the left and right children.
4) Return the result.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def evaluate_tree(root):
if not root:
return 0
# If the node is a leaf node, return its value (an integer)
if not root.left and not root.right:
return root.val
# Otherwise, evaluate the left and right children
left_val = evaluate_tree(root.left)
right_val = evaluate_tree(root.right)
# Apply the operation based on the node's value
if root.val == '+':
return left_val + right_val
elif root.val == '-':
return left_val - right_val
elif root.val == '*':
return left_val * right_val
elif root.val == '/':
return left_val / right_val
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
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 we need to traverse all the nodes in the tree.O(N)
for the recursion stack in the worst case when the tree is completely unbalanced.