Subtree of Another Tree

 

  1. Clarify the problem:

    • The problem requires checking whether one binary tree is a subtree of another binary tree.
    • We need to implement a function that takes two binary tree nodes as input and returns a boolean indicating whether the second tree is a subtree of the first tree.
  2. Analyze the problem:

    • Input: Two binary tree nodes representing the root nodes of the trees.
    • Output: A boolean indicating whether the second tree is a subtree of the first tree.
    • Constraints: None given.
  3. Design an algorithm:

    • We can use a recursive approach to solve this problem.
    • We will implement a helper function, isMatch, that checks whether two trees are identical.
    • In the main function, isSubtree, we will perform a depth-first search on the first tree to find a node that matches the root of the second tree.
    • Whenever we find a matching node, we will call the isMatch function to check if the subtrees rooted at those nodes are identical.
    • If we find a matching subtree, we will return True. Otherwise, we continue the search until we have traversed the entire first tree.
    • If no matching subtree is found, we return False.
  4. Explain your approach:

    • We will implement a helper function called isMatch that checks whether two trees are identical.
    • The isMatch function takes two tree nodes, s and t, as input.
    • If both nodes are None, we return True since they are considered identical.
    • If either node is None or their values differ, we return False.
    • We recursively call the isMatch function on the left and right subtrees to check their identicalness.
    • In the main function, isSubtree, we will perform a depth-first search on the first tree.
    • Whenever we find a node with the same value as the root of the second tree, we call the isMatch function to check if the subtrees rooted at those nodes are identical.
    • If we find a matching subtree, we return True. Otherwise, we continue the search until we have traversed the entire first tree.
    • If no matching subtree is found, we return False.
  5. Write clean and readable code:

    python
  6. class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def isMatch(s, t): if s is None and t is None: return True if s is None or t is None or s.val != t.val: return False return isMatch(s.left, t.left) and isMatch(s.right, t.right) def isSubtree(s, t): if s is None: return False if isMatch(s, t): return True return isSubtree(s.left, t) or isSubtree(s.right, t)
  7. Test your code:

    python
  8. # Test case 1 s = TreeNode(3) s.left = TreeNode(4) s.right = TreeNode(5) s.left.left = TreeNode(1) s.left.right = TreeNode(2) t = TreeNode(4) t.left = TreeNode(1) t.right = TreeNode(2) # The subtree rooted at node with value 4 is identical to the second tree assert isSubtree(s, t) == True # Test case 2 s = TreeNode(3) s.left = TreeNode(4) s.right = TreeNode(5) s.left.left = TreeNode(1) s.left.right = TreeNode(2) t = TreeNode(4) t.left = TreeNode(1) t.right = TreeNode(3) # No subtree is identical to the second tree assert isSubtree(s, t) == False # Test case 3 s = TreeNode(1) s.left = TreeNode(2) s.right = TreeNode(3) t = TreeNode(1) t.left = TreeNode(2) t.right = TreeNode(3) # The entire tree s is identical to the second tree assert isSubtree(s, t) == True

    I chose these test cases to cover different scenarios:

    • Test case 1 checks if a subtree with multiple levels is identified correctly.
    • Test case 2 checks if a non-matching subtree is correctly identified.
    • Test case 3 checks if the entire tree is identified as a subtree.
  9. Optimize if necessary:

    • The algorithm has a time complexity of O(m * n) in the worst case, where m and n are the number of nodes in the first and second trees, respectively.
    • There is no further optimization possible for this problem.
  10. Handle error cases:

    • The code assumes that the input trees are valid binary trees, but it does not handle cases where the input trees are None or do not follow the binary tree structure.
  11. Discuss complexity analysis:

    • Let m and n be the number of nodes in the first and second trees, respectively.
    • The time complexity of the solution is O(m * n) in the worst case since we may need to compare each node in the first tree with each node in the second tree.
    • The space complexity is O(max(m, n)) due to the recursion stack used by the depth-first search.
    • The code solves the problem optimally without any significant trade-offs.
Next Post Previous Post