![]() |
VOOZH | about |
Given an N-ary tree root, the task is to check if it is symmetric horizontally (Mirror image of itself).
Example:
Input: root = 7
/ / \ \
4 2 2 4
/ | / | | \ | \
3 2 6 7 7 6 2 3
Output: true
Explanation: The left side of the tree is a mirror image of the right sideInput: root= 2
/ | \
3 4 3
/ | \
5 6 5
Output: true
Approach: The given problem can be solved by using the preorder traversal. The idea is to pass two root nodes as parameters and check if the value of the current nodes is the same and use recursion to check if the value of children of the left node is the same as the values of children of the right node.
Below steps can be followed to solve the problem:
Below is the implementation of the above approach:
true
Time Complexity: O(N), where N is the number of nodes in the tree
Auxiliary Space: O(H), Where H is the height of the tree