主要实现二叉树的左右子树的一个翻转过程
#include <iostream>
#include<queue>
using namespace std;
struct BinaryTreeNode
{
int value;
BinaryTreeNode *lchild,*rchild;
};
//利用递归方法
void mirrorRecursively(BinaryTreeNode *root)
{
if(root==NULL||root->lchild==NULL||root->lchild==NULL)
return ;
BinaryTreeNode *temp=root->lchild;
root->lchild=root->rchild;
root->rchild=temp;
if(root->lchild!=NULL)
mirrorRecursively(root->lchild);
if(root->rchild!=NULL)
mirrorRecursively(root->rchild);
return ;
}
//利用循环非递归方法
void mirrorNonrecursively(BinaryTreeNode *root)
{
if(root==NULL||root->rchild==NULL||root->lchild==NULL)
return ;
queue<BinaryTreeNode*>q;
q.push(root);
while(!q.empty())
{
BinaryTreeNode *r=q.front();
q.pop();
BinaryTreeNode *temp=r->lchild;
r->lchild=r->rchild;
r->rchild=temp;
if(r->lchild!=NULL)
q.push(r->lchild);
if(r->rchild!=NULL)
q.push(r->rchild);
}
return ;
}