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
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.