Check if the given n-ary tree is symmetric tree or not
Write a program to check if the given n-ary tree is symmetric tree or not. A symmetric tree is defined as a tree which is mirror image of itself about the root node. For example, following tree is a symmetric tree.
Video coming soon!
Subscribe for more updates
Preparing for interviews? IDeserve team is here to help.
Create your profile
Create your profile, and here is what you will get:
1: Interview practice platform.
2: Once you are ready to take the interview, IDeserve team will help you get connected to the best job opportunities.
3: Personalized mentorship from IDeserve team once your interview process has started.
Creation of profile shouldn't take more than 2 minutes.
The algorithm is an implementation of a simple idea that - 1. For given two trees, if both trees are empty then they are mirror images of one another. Else they have to satisfy following conditions: 2. Root values of both trees have to be same. This can be checked by comparing values of root nodes for given two trees. 3. Sub-tree rooted at the first position should be mirror image of sub-tree rooted at the last position, sub-tree rooted at second position should be mirror image of the sub-tree rooted at second last-position and so on. This can be checked by making recursive calls.
Initial call to checkSymmetry(TreeNode root1, TreeNode root2) is made with root1 = root and root2 = root.