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?
guest1
and guest2
are None
?
True
since both trees are identical (empty).None
and the other is not?
False
since the trees are not identical.False
because the structure matters.HAPPY CASE
Input: guest1 = TreeNode("John Doe", TreeNode("6 ft"), TreeNode("Brown Eyes")),
guest2 = TreeNode("John Doe", TreeNode("6 ft"), TreeNode("Brown Eyes"))
Output: True
Explanation: The trees have identical structure and values.
Input: guest3 = TreeNode("John Doe", TreeNode("6 ft")),
guest4 = TreeNode("John Doe", None, TreeNode("6 ft"))
Output: False
Explanation: The trees have the same values but different structures, so they are not clones.
EDGE CASE
Input: guest1 = None, guest2 = None
Output: True
Explanation: Both trees are empty, so they are clones.
Input: guest1 = TreeNode("John Doe"), guest2 = None
Output: False
Explanation: One tree is empty, and the other is not, so they are not clones.
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 comparing two binary trees, we can consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
1) Base Cases:
guest1
and guest2
are None
, return True
(both trees are empty).guest1
or guest2
is None
, return False
(one tree is empty, and the other is not).
2) Node Comparison:guest1
and guest2
have the same value.True
if all checks pass, otherwise return False
.Pseudocode:
1) Define the base cases:
* If both `guest1` and `guest2` are `None`, return `True`.
* If only one of `guest1` or `guest2` is `None`, return `False`.
2) Check if `guest1.val` is equal to `guest2.val`.
3) Recursively compare the left subtrees of `guest1` and `guest2`.
4) Recursively compare the right subtrees of `guest1` and `guest2`.
5) Return `True` if all checks pass, otherwise return `False`.
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, value, left=None, right=None):
self.val = value
self.left = left
self.right = right
def is_clone(guest1, guest2):
# Base cases
if not guest1 and not guest2:
return True
if not guest1 or not guest2:
return False
# Check if the current nodes have the same value and
# recursively check left and right subtrees
return (guest1.val == guest2.val and
is_clone(guest1.left, guest2.left) and
is_clone(guest1.right, guest2.right))
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
guest1 = TreeNode("John Doe", TreeNode("6 ft"), TreeNode("Brown Eyes"))
and guest2 = TreeNode("John Doe", TreeNode("6 ft"), TreeNode("Brown Eyes"))
:
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in the trees.
O(N)
because each node in both trees must be visited once.O(N)
due to the recursive call stack in the worst case, assuming a balanced tree.