Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
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
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

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
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

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

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close

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.

Example One

Output:

1

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

Example Two

Output:

0

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

Example Three

Output:

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:

To learn more, register for the FREE webinar.

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.

Example One

Output:

1

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

Example Two

Output:

0

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

Example Three

Output:

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:

To learn more, register for the FREE webinar.

Worried About Failing Tech Interviews?

Attend our free webinar to amp up your career and get the salary you deserve.

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Accelerate your Interview prep with Tier-1 tech instructors
blue tick
360° courses that have helped 14,000+ tech professionals
blue tick
100% money-back guarantee*
Register for Webinar
All Blog Posts