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: root = TreeNode(3, TreeNode(4, TreeNode(1), TreeNode(2)), TreeNode(5)), sub_root = TreeNode(4, TreeNode(1), TreeNode(2))
Output: True
Explanation: The subtree starting with node 4 matches the structure and values of sub_root.
EDGE CASE
Input: root = TreeNode(1), sub_root = None
Output: True
Explanation: An empty subtree is a subtree of any tree, including a single-node tree.
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 essentially a tree traversal and matching problem, involving checking each node for a match with the subtree's root and verifying subsequent structure and values.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the main tree to find a node that matches the root of the subtree and then verify that the entire subtree structure matches from that point.
1) If either the main tree or the subtree root is null, handle base cases.
2) For each node in the main tree that matches the subtree's root, check if the subtree starting from this node matches the given subtree.
3) Use a helper function to compare two trees for equality.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def is_subtree(root, sub_root):
"
Determine if `sub_root` is a subtree of `root`.
"
if not sub_root:
return True # An empty subtree is a subtree of any tree
if not root:
return False # Non-empty subtree cannot be a subtree of an empty tree
if is_same_tree(root, sub_root):
return True
return is_subtree(root.left, sub_root) or is_subtree(root.right, sub_root)
def is_same_tree(s, t):
"
Helper function to determine if two trees are identical.
"
if not s and not t:
return True
if not s or not t:
return False
return s.val == t.val and is_same_tree(s.left, t.left) and is_same_tree(s.right, t.right)
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 * m)
where n is the number of nodes in the main tree and m is the number of nodes in the subtree. This reflects the worst-case scenario where each node in the main tree needs to be compared to the subtree.O(h)
where h is the height of the main tree, which influences the recursion stack depth.