Register for our webinar

### How to Nail your next Technical Interview

1 hour
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
Step 1
Step 2
Congratulations!
You have registered for our webinar
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

## You may be missing out on a 66.5% salary hike*

### Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Help us know you better!

## How many years of coding experience do you have?

Oops! Something went wrong while submitting the form.

## FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Oops! Something went wrong while submitting the form.

# Symmetric Tree

A symmetric tree is a mirror image of itself around the root node. A recursive or iterative approach can determine whether or not a binary tree is symmetric. Let’s take a look at the examples of Symmetric Tree and determine whether they’re true or false.

## Symmetric Tree Problem Statement

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

### Output:

1

Learn how to Construct the Binary Tree with the inorder and preorder traversal.

### Output:

0

Learn how to solve the Mirror Image of Binary Tree problem.

0

### Notes

#### Constraints:

• 0 <= number of nodes in the tree <= 5 * 104
• 0 <= value in a tree node <= 105

Know how to Validate a Binary Search Tree.

## Code for Symmetric Tree:

Following is the code to solve the symmetric tree problem.

``````
/*

Asymptotic complexity in terms of the number of nodes ( = `n`) and the height ( = `h`) of the tree rooted at `root`:

* Time: O(n).

* Auxiliary space: O(h).

* Total space: O(n).

*/

// Helper function which returns true if trees with roots as root1 and root2 are mirror images.

bool check_if_mirror(BinaryTreeNode* root1, BinaryTreeNode* root2){

if (root1 == NULL && root2 == NULL){

return true;

}

if(root1 == NULL || root2 == NULL){

return false;

}

if (root1->value == root2->value){

return check_if_mirror(root1->left, root2->right) && check_if_mirror(root1->right, root2->left);

}

return false;

}

bool check_if_symmetric(BinaryTreeNode *root) {

return check_if_mirror(root, root);

}

``````

Find out how to Build a Balanced BST from a Sorted Array.

We hope that these solutions to the Symmetric Tree problem will help you level up your coding skills. Data structures are the foundation of programming languages, and the Binary Search Tree is a tree that allows for fast search, insert, and delete operations on sorted data. It also allows you to find the item that is closest to you. Routing Tables, Sorting, Decision Trees, and Data Compression are just a few of the many applications of Symmetric Tree.

Because of the widespread use of Tree Data Structure, companies such as Amazon and Zomato include Symmetric Tree interview questions and other Binary Tree Data Structure problems in their tech interviews.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart’s FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, FAANG+ instructors, and career coaching to help you nail your next tech interview.

We offer 17 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:

### Try yourself in the Editor

Note: Input and Output will already be taken care of.

# Symmetric Tree

A symmetric tree is a mirror image of itself around the root node. A recursive or iterative approach can determine whether or not a binary tree is symmetric. Let’s take a look at the examples of Symmetric Tree and determine whether they’re true or false.

## Symmetric Tree Problem Statement

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

### Output:

1

Learn how to Construct the Binary Tree with the inorder and preorder traversal.

### Output:

0

Learn how to solve the Mirror Image of Binary Tree problem.

0

### Notes

#### Constraints:

• 0 <= number of nodes in the tree <= 5 * 104
• 0 <= value in a tree node <= 105

Know how to Validate a Binary Search Tree.

## Code for Symmetric Tree:

Following is the code to solve the symmetric tree problem.

``````
/*

Asymptotic complexity in terms of the number of nodes ( = `n`) and the height ( = `h`) of the tree rooted at `root`:

* Time: O(n).

* Auxiliary space: O(h).

* Total space: O(n).

*/

// Helper function which returns true if trees with roots as root1 and root2 are mirror images.

bool check_if_mirror(BinaryTreeNode* root1, BinaryTreeNode* root2){

if (root1 == NULL && root2 == NULL){

return true;

}

if(root1 == NULL || root2 == NULL){

return false;

}

if (root1->value == root2->value){

return check_if_mirror(root1->left, root2->right) && check_if_mirror(root1->right, root2->left);

}

return false;

}

bool check_if_symmetric(BinaryTreeNode *root) {

return check_if_mirror(root, root);

}

``````

Find out how to Build a Balanced BST from a Sorted Array.

We hope that these solutions to the Symmetric Tree problem will help you level up your coding skills. Data structures are the foundation of programming languages, and the Binary Search Tree is a tree that allows for fast search, insert, and delete operations on sorted data. It also allows you to find the item that is closest to you. Routing Tables, Sorting, Decision Trees, and Data Compression are just a few of the many applications of Symmetric Tree.

Because of the widespread use of Tree Data Structure, companies such as Amazon and Zomato include Symmetric Tree interview questions and other Binary Tree Data Structure problems in their tech interviews.

If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart’s FREE webinar to understand the best way to prepare.

Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, FAANG+ instructors, and career coaching to help you nail your next tech interview.

We offer 17 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as: