Tutorialspoint
Problem
Solution
Submissions

Symmetric Tree

Certification: Basic Level Accuracy: 0% Submissions: 0 Points: 5

Write a C program to check if a binary tree is symmetric around its center, meaning it is a mirror of itself. A binary tree is symmetric if the left subtree of the root is a mirror reflection of the right subtree of the root.

Example 1
  • Input:

    Symmetric Tree - Symmetricity


  • Output: true
  • Explanation:
    • The left subtree of the root (1) consists of nodes 2, 3, and 4.
    • The right subtree of the root (1) consists of nodes 2, 4, and 3.
    • The left subtree is a mirror reflection of the right subtree.
    • The values match in mirror positions (2-2, 3-3, 4-4).
    • Therefore, the tree is symmetric.
Example 2
  • Input:

    Symmetric Tree - Not Symmetric

  • Output: false
  • Explanation:
    • The left subtree of the root (1) consists of nodes 2 and 3.
    • The right subtree of the root (1) consists of nodes 2 and 3.
    • In the left subtree, node 3 is the right child of node 2.
    • In the right subtree, node 3 is also the right child of node 2.
    • In a mirror reflection, node 3 in the right subtree should be the left child of node 2.
    • Since this is not the case, the tree is not symmetric.
Constraints
  • The number of nodes in the tree is in the range [1, 1000].
  • The values of nodes are integers in the range [-100, 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 (due to recursion stack).
TreePwCPhillips
Editorial

Login to view the detailed solution and explanation for this problem.

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.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a recursive approach to check if two subtrees are mirror images of each other.
  • Start by comparing the left and right subtrees of the root.
  • Two subtrees are mirror images if: a. Their root values are the same b. The left subtree of one is a mirror of the right subtree of the other c. The right subtree of one is a mirror of the left subtree of the other
  • Handle the base cases properly (e.g., when both nodes are NULL).
  • You can also use an iterative approach with a queue or stack if preferred.
  • Be careful about the comparison conditions to ensure proper mirroring.
  • Consider edge cases like empty trees or trees with only one node.

Steps to solve by this approach:

 Step 1: Create a helper function isMirror() that checks if two subtrees are mirror images of each other.

 Step 2: In the isMirror() function, handle the base cases: - If both trees are NULL, return true (empty trees are mirrors of each other). - If only one tree is NULL, return false (they cannot be mirrors).
 Step 3: Check if the values of the current nodes are equal.
 Step 4: Recursively check if the left subtree of the first tree is a mirror of the right subtree of the second tree.
 Step 5: Recursively check if the right subtree of the first tree is a mirror of the left subtree of the second tree.
 Step 6: In the isSymmetric() function, handle the case of an empty tree (return true).
 Step 7: For non-empty trees, call the isMirror() function with the left and right subtrees of the root.

Submitted Code :