本文为镜像二叉树的C++代码,为了代码简洁,将空节点和某个节点具有空孩子的情况放到了同一个逻辑下进行判断。
typedef struct { int value; BTNode *pLeft; BTNode *pRight; }BTNode; void BTMirror(BTNode *pHead) { if (pHead == NULL) return; BTNode *pTmp; pTmp = pHead->pLeft; pHead->pLeft = pHead->pRight; pHead->pRight = pTmp; BTMirror(pHead->pLeft); BTMirror(pHead->pRight); }