Bloomberg 面试题05012012[3]

Write a function to create an mirror image of binary tree


#include <iostream>
#include <queue>

struct node
{
	int data;
	node* left;
	node* right;
	node(int _data, node* _left, node* _right)
	{
		data = _data;
		left = _left;
		right = _right;
	}
};


void createMirror(node* n)
{
	if(n == NULL)
		return;

	node* temp = n->left;
	n->left = n->right;
	n->right = temp;

	createMirror(n->left);
	createMirror(n->right);
}

int main()
{
	node* n7 = new node(7, NULL, NULL);
	node* n6 = new node(6, NULL, NULL);
	node* n5 = new node(5, NULL, NULL);
	node* n4 = new node(4, NULL, NULL);
	node* n3 = new node(3, n6, n7);
	node* n2 = new node(2, n4, n5);
	node* n1 = new node(1, n2, n3);
	node* root = n1;

	createMirror(root);

	return 0;
}


你可能感兴趣的:(Bloomberg 面试题05012012[3])