Mirror image of a binary tree I wrote a small routines to create a mirror for any binary tree. I am just sharing my necessary routines instead of full class. could you please let me know your comments on this. I just curious to find the complexity of this but I am new to algorithms.

void tree_sample::mirror_image()
{
    create_mirror_for_left_subtree(root-&gt;left);
    create_mirror_for_right_subtree(root-&gt;right);
    tree_node* temp = 0;
    if(root-&gt;left)
        temp = root-&gt;left;
    root-&gt;left = root-&gt;right;
    if(temp)
        root-&gt;right = temp;
    else
        root-&gt;right = NULL;     
}

void tree_sample::create_mirror_for_left_subtree(tree_node *temp)
{
    while(temp!=NULL)
    {
        do_mirror_for_inidividual_node(temp);
        temp = temp-&gt;left;
    }
}

void tree_sample::create_mirror_for_right_subtree(tree_node *temp)
{
    while(temp!=NULL)
    {
        do_mirror_for_inidividual_node(temp);
        temp = temp-&gt;right;
    }
}

void tree_sample::do_mirror_for_inidividual_node(tree_node *temp)
{
    tree_node* temp2 = 0;
    if(temp-&gt;left)
        temp2 = temp-&gt;left;
    temp-&gt;left = temp-&gt;right;
    if(temp2)
        temp-&gt;right = temp2;
    else
        temp-&gt;right= 0;
}
I think you have over complicated things and I don't think it works for the generic tree (as your create_mirror_for_[left|right]_subtree() only recurse down one side of the tree.

Why the test before a swap?

tree_node* temp = 0;
if(root-&gt;left)
    temp = root-&gt;left;    // If left is NULL you can still assign it 
                          // to temp and get the same result.
root-&gt;left = root-&gt;right;
if(temp)
    root-&gt;right = temp;
else
    root-&gt;right = NULL;   // If temp is null you could just assign
                          // it to right then both sides of the branch
                          // are identical and they collapse into a
                          // single statement.

// Much easier to write as:
tree_node* temp = root-&gt;left;
root-&gt;left      = root-&gt;right;
root-&gt;right     = temp;

// Which of course can be replaced by std::swap
std::swap(root-&gt;left, root-&gt;right);


I assume you want to swap all nodes in the tree (otherwise it's not a mirror).

class Tree
{
    public:
        void mirrorTree()
        {
            root.mirrorTree(root);
        }
    private:
        void mirrorTree(Node* node)
        {
            if (node == nullptr) {
                return;
            }
            using std::swap;
            swap(node-&gt;left, node-&gt;right);
            mirrorTree(node-&gt;left);
            mirrorTree(node-&gt;right);
        }
};