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(5, TreeNode(5), TreeNode(5))
Output: True
Explanation: All nodes have the value 5, meeting the criteria of a uni-valued tree.
EDGE CASE
Input: None
Output: True
Explanation: An empty tree is considered uni-valued as there are no differing values.
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 problem is a tree property validation challenge, often used in scenarios assessing data consistency across structured data.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Recursively verify that every node in the tree has the same value as the root node.
1) If the tree is empty, return True.
2) Start at the root and compare each node's value with the root’s value as you traverse the tree.
3) If all nodes match the root's value, return True; otherwise, return False.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def is_univalued_helper(node, value):
"
Helper function to determine if the current node and all its descendants have the same value.
"
if not node:
return True
if node.val != value:
return False
return is_univalued_helper(node.left, value) and is_univalued_helper(node.right, value)
def is_univalued(root):
"
Check if a binary tree is uni-valued, i.e., all nodes contain the same value.
"
if not root:
return True
return is_univalued_helper(root, root.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.
O(n)
where n is the number of nodes in the tree, as each node is checked for value uniformity.O(h)
where h is the height of the tree due to recursion depth, potentially reaching O(n)
in a skewed tree.