//题目描述:将一个二叉树处理为自身的镜像
//思路:递归,将左右子树互换
//树节点的结构
struct BTreeNode{
int value;
BTreeNode* pLeft;
BTreeNode* pRight;
}
void getMirrorBTree(BTreeNode* pRoot){
//检验输入的合法性,增强程序鲁棒性,也同时是递归的终止条件
if(pRoot==NULL||(pRoot->pLeft==NULL&&pRoot->pRight==NULL)){
return;
}
//左右子树互换
BTreeNode* pTemp = pRoot->pLeft;
pRoot->pLeft = pRoot->pRight;
pRoot->pRight = pTemp;
//递归
if(pRoot->pLeft){
getMirrorBTree(pRoot->pLeft);
}
if(pRoot->pRight){
getMirrorBTree(pRoot->pRight);
}
}
二叉树镜像--20150924
最新推荐文章于 2021-11-22 16:19:00 发布