Mirror Tree
Given a Binary Tree, convert it into its mirror.
Example 1:
Input:
1
/ \
2 3
Output: 2 1 3
Explanation: The tree is
1 (mirror) 1
/ \ => / \
3 2 2 3
The inorder of mirror is 2 1 3
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output: 30 10 60 20 40
Explanation: The tree is
10 10
/ \ (mirror) / \
20 30 => 30 20
/ \ / \
40 60 60 40
The inroder traversal of mirror is
30 10 60 20 40.
c++ implementation:
void mirror(Node* node)
{
queue<Node*>q; Node*we;
q.push(node);
while(!q.empty())
{ Node* t=q.front();
q.pop();
we=t->left;
t->left=t->right;
t->right=we;
if(t->left!=NULL)
q.push(t->left);
if(t->right!=NULL)
q.push(t->right);
}
}
time complexity: O(N)
No comments