Clone Detection - codepath/compsci_guides GitHub Wiki

Unit 9 Session 1 (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Medium
  • Time to complete: 15 mins
  • 🛠️ Topics: Binary Trees, Recursion, Tree Comparison

1: U-nderstand

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 both guest1 and guest2 are None?
    • Return True since both trees are identical (empty).
  • What if one tree is None and the other is not?
    • Return False since the trees are not identical.
  • Can the tree nodes have identical values but different structures?
    • Yes, and in such cases, the function should return 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.

2: M-atch

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:

  • Recursion: Use recursion to compare nodes in corresponding positions of both trees.
  • Tree Traversal: Traverse both trees in a pre-order, in-order, or post-order manner, and compare the nodes at each step.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

Plan

  1. Base Cases:
    • If both guest1 and guest2 are None, return True (both trees are empty).
    • If only one of guest1 or guest2 is None, return False (one tree is empty, and the other is not).
  2. Node Comparison:
    • Check if the current nodes of guest1 and guest2 have the same value.
    • Recursively check the left subtrees of both trees.
    • Recursively check the right subtrees of both trees.
  3. Return True if all checks pass, otherwise return False.

Recursive Implementation

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`.

4: I-mplement

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))

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Trace through your code with the input guest1 = TreeNode("John Doe", TreeNode("6 ft"), TreeNode("Brown Eyes")) and guest2 = TreeNode("John Doe", TreeNode("6 ft"), TreeNode("Brown Eyes")):
    • The trees should be correctly identified as clones.

6: E-valuate

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.

  • Time Complexity: O(N) because each node in both trees must be visited once.
  • Space Complexity: O(N) due to the recursive call stack in the worst case, assuming a balanced tree.