C# || How To Determine Whether A Binary Tree Is A Symmetric Tree Using C#

Print Friendly, PDF & Email

The following is a module with functions which demonstrates how to determine whether a binary tree is a symmetric tree using C#.


1. Is Symmetric – Problem Statement

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Example 1:

Example 1


Input: root = [1,2,2,3,4,4,3]
Output: true

Example 2:

Example 2


Input: root = [1,2,2,null,3,null,3]
Output: false


2. Is Symmetric – Solution

The following is a solution which demonstrates how to determine whether a binary tree is a symmetric tree.

For two trees to be mirror images, the following three conditions must be true:


• 1 - Their root node's key must be same
• 2 - The left subtree of left tree and right subtree of right tree have to be mirror images
• 3 - The right subtree of left tree and left subtree of right tree have to be mirror images

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


true
false

Was this article helpful?
👍 YesNo

Leave a Reply