题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述: 二叉树的镜像定义:源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5
#include <iostream>
using namespace std;
struct TreeNode{
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x):val(x),left(NULL),right(NULL){}
};
void Mirror(TreeNode *pRoot){
if(pRoot!=NULL){
TreeNode *temp;
temp=pRoot->left;
pRoot->left=pRoot->right;
pRoot->right=temp;
Mirror(pRoot->left);
Mirror(pRoot->right);
}
}
void printTree(TreeNode *T){
if(T!=NULL){
cout<<T->val<<" ";
printTree(T->left);
printTree(T->right);
}
}
int main(){
TreeNode n1(8),n2(6),n3(10),n4(5),n5(7),n6(9),n7(11);
TreeNode *pRoot=&n1;
n1.left=&n2;
n1.right=&n3;
n2.left=&n4;
n2.right=&n5;
n3.left=&n6;
n3.right=&n7;
printTree(pRoot);
Mirror(pRoot);
cout<<endl;
printTree(pRoot);
system("pause");
}