Coral Reef Symmetry - codepath/compsci_guides GitHub Wiki
"Unit 8 Session 1 Advanced (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10 mins
- 🛠️ Topics: Binary Tree, Recursion, Tree Traversal
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 does it mean for a binary tree to be symmetric?
- A binary tree is symmetric if its left and right subtrees are mirror images of each other.
- How should the function behave if the tree is empty?
- The function should return
True
since an empty tree is trivially symmetric.
- The function should return
HAPPY CASE
Input:
Tree structure:
A
/ \
B B
/ \ / \
C D D C
Output: True
Explanation: The tree is symmetric.
EDGE CASE
Input:
Tree structure:
A
/ \
B B
/ \ / \
C D C D
Output: False
Explanation: The tree is not symmetric.
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 Tree Symmetry problems, we want to consider the following approaches:
- Binary Tree Traversal: Traverse the tree to check if the left and right subtrees are mirror images.
- Recursion: Use recursion to compare nodes in the left and right subtrees.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the tree recursively, comparing the left and right subtrees to see if they are mirror images.
1) If the root is None, return True (an empty tree is symmetric).
2) Create a helper function `is_mirror` that takes two nodes (left and right).
3) If both left and right nodes are None, return True.
4) If one node is None and the other is not, return False.
5) If the values of the left and right nodes are not equal, return False.
6) Recursively check if the left subtree's left child is a mirror of the right subtree's right child and if the left subtree's right child is a mirror of the right subtree's left child.
7) Return the result of the recursive checks.
⚠️ Common Mistakes
- Not correctly handling the case where the tree is empty.
- Failing to compare corresponding nodes in the left and right subtrees.
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_symmetric(root):
if root is None:
return True
def is_mirror(left, right):
if left is None and right is None:
return True
if left is None or right is None:
return False
if left.val != right.val:
return False
return is_mirror(left.left, right.right) and is_mirror(left.right, right.left)
return is_mirror(root.left, root.right)
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
-
Test with the examples given:
- Input 1: Tree structure: A / \ B B / \ / \ C D D C - Expected Output: True
- Input 2: Tree structure: A / \ B B / \ / \ C D C D - Expected Output: False
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 binary tree.
- Time Complexity:
O(N)
because the algorithm needs to visit each node in the tree. - Space Complexity:
O(H)
whereH
is the height of the tree, due to the recursive call stack."