数据结构练习(10)求二元查找树的镜像

http://zhedahht.blog.163.com/blog/static/2541117420072159363370/

#include <stack>

struct Node {
    int m_value;
    Node *m_left;
    Node *m_right;
};

void getmirror(Node *root)
{
    if (root == NULL)
        return ;

    Node *node = root->m_left;
    root->m_left = root->m_right;
    root->m_right = node;

    getmirror(root->m_left);
    getmirror(root->m_right);
}

void getmirror_2(Node *root)
{
    if (root == NULL)
        return ;

    std::stack<Node*> stacknode;
    
    stacknode.push(root);
    while (stacknode.size())
    {
        Node *node = stacknode.top();
        stacknode.pop();

        if (node->m_left)
            stacknode.push(node->m_left);
        if (node->m_right)
            stacknode.push(node->m_right);

        Node *temp = node->m_right;
        node->m_right = node->m_left;
        node->m_left = temp;
    }
}

 

posted @ 2012-12-12 14:36  kedebug  阅读(189)  评论(0编辑  收藏  举报