
Problem
Solution
Submissions
Symmetric Tree Check
Certification: Basic Level
Accuracy: 100%
Submissions: 1
Points: 8
Write a Java program to check if a binary tree is symmetric around its center (i.e., mirror of itself). A binary tree is symmetric if the left subtree is a mirror reflection of the right subtree.
Example 1
- Input: root = [1,2,2,3,4,4,3]
- Output: true
- Explanation:
- The tree is symmetric around its center.
Example 2
- Input: root = [1,2,2,null,3,null,3]
- Output: false
- Explanation:
- The tree is not symmetric.
Constraints
- The number of nodes in the tree is in the range [1, 1000].
- -100 ≤ Node.val ≤ 100
- Time Complexity: O(n), where n is the number of nodes in the tree.
- Space Complexity: O(h), where h is the height of the tree.
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a recursive approach to check if two subtrees are mirrors of each other.
- Two trees are a mirror reflection if: - Their root values are the same. - The left subtree of one tree is a mirror of the right subtree of the other.
- Check if the root's left and right subtrees are mirrors of each other.
- Handle base cases like empty trees or single-node trees.