题目:输入一颗二元查找树,将该树转换为它的镜像,
即在转换后的二元查找树中,左子树的结点都大于右子树的结点。
用递归和循环两种方法完成树的镜像转换。
例如输入:
8
/ \
6 10
/ \ / \
5 7 9 11
输出:
8
/ \
10 6
/ \ / \
11 9 7 5
具体实现代码如下:
void mirror(Node* root)
{
Node* const left = root->lchild;
Node* const right = root->rchild;
root->lchild = right;
root->rchild = left;
if (left) mirror(left);
if (right) mirror(right);
}