Unit 8 Session 2 (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?
HAPPY CASE
Input: TreeNode(4, TreeNode(6), TreeNode(8))
Output: True
Explanation: All node values (4, 6, 8) are even.
EDGE CASE
Input: TreeNode(4, TreeNode(7), TreeNode(8))
Output: False
Explanation: Not all node values are even; the node with value 7 is odd.
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.
This is a tree traversal problem where the task is to check a property (evenness of node values) across all nodes in a binary tree.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the tree using a recursive approach to check if all node values are even.
1) If the tree is empty, return True.
2) Check if the value of the current node is even.
3) Recursively verify that both the left and right subtree nodes are also even.
4) Return False immediately if any node is found with an odd value.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def is_even_helper(node):
"
Helper function to determine if the current node and all its descendants have even values.
"
if not node:
return True
# Check if the current node's value is even
if node.val % 2 != 0:
return False
# Recursively check both subtrees
return is_even_helper(node.left) and is_even_helper(node.right)
def is_even(root):
"
Check if all nodes in the binary tree contain even values.
"
return is_even_helper(root)
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.
O(n)
where n is the number of nodes in the tree, as each node must be checked.O(h)
where h is the height of the tree due to recursion, which could be O(n)
in a skewed tree.