14. Symmetric Tree
β GFG solution to the Symmetric Tree problem: check if a binary tree is symmetric using recursive mirror comparison. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given the root of a binary tree, check whether it is symmetric, i.e., whether the tree is a mirror image of itself.
A binary tree is symmetric if the left subtree is a mirror reflection of the right subtree.
π Examples
Example 1
Input: root[] = [1, 2, 2, 3, 4, 4, 3]
1
/ \
2 2
/ \ / \
3 4 4 3
Output: True
Explanation: As the left and right half of the above tree is mirror image, tree is symmetric.
Example 2
Input: root[] = [1, 2, 2, N, 3, N, 3]
1
/ \
2 2
\ \
3 3
Output: False
Explanation: As the left and right half of the above tree is not the mirror image, tree is not symmetric.
π Constraints
$1 \le \text{number of nodes} \le 2000$
β
My Approach
The optimal approach uses Recursive Mirror Comparison to check if the left and right subtrees are mirror images of each other:
Recursive Mirror Comparison
Base Cases:
If root is null, the tree is symmetric by definition.
If both nodes being compared are null, they are symmetric.
If one node is null and the other isn't, they are not symmetric.
Value Comparison:
If the data values of the two nodes don't match, they are not symmetric.
Recursive Mirror Check:
Compare left subtree of first node with right subtree of second node.
Compare right subtree of first node with left subtree of second node.
Both comparisons must return true for the tree to be symmetric.
Main Function Logic:
Handle the null root case directly.
Call the helper function with
root->left
androot->right
.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the number of nodes in the tree. We visit each node exactly once during the recursive traversal.
Expected Auxiliary Space Complexity: O(h), where h is the height of the tree. This is due to the recursive call stack, which can go as deep as the height of the tree. In the worst case (skewed tree), h = n, making it O(n).
π§βπ» Code (C++)
class Solution {
bool isSym(Node* a, Node* b){
if(!a||!b) return a==b;
if(a->data!=b->data) return false;
return isSym(a->left,b->right)&&isSym(a->right,b->left);
}
public:
bool isSymmetric(Node* r){
return !r||isSym(r->left,r->right);
}
};
π§βπ» Code (Java)
class Solution {
boolean isSym(Node a, Node b){
if(a==null||b==null) return a==b;
if(a.data!=b.data) return false;
return isSym(a.left,b.right)&&isSym(a.right,b.left);
}
public boolean isSymmetric(Node r){
return r==null||isSym(r.left,r.right);
}
}
π Code (Python)
class Solution:
def isSym(self,a,b):
if not a or not b: return a is b
if a.data!=b.data: return False
return self.isSym(a.left,b.right) and self.isSym(a.right,b.left)
def isSymmetric(self, root):
return root is None or self.isSym(root.left,root.right)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated