Question : Write a function to mirror a binary tree.
The mirror tree will have a left subtree and right subtree swapped. But this has to be done for each and every node.
So to convert a tree to its mirror, we can write an algorithm like this
1) mirror left subtree recursively
2) mirror right subtree recursively
3) swap left and right children of the current node.
Here is the function for it.
The mirror tree will have a left subtree and right subtree swapped. But this has to be done for each and every node.
So to convert a tree to its mirror, we can write an algorithm like this
1) mirror left subtree recursively
2) mirror right subtree recursively
3) swap left and right children of the current node.
Here is the function for it.
void mirror_tree(NODEPTR root) { if(root!=NULL) { NODEPTR temp = root->left; root->left = root->right; root->right = temp; mirror_tree(root->left); mirror_tree(root->right); } }
Comments
Post a Comment