C++递归解法
二叉树的镜像
http://www.nowcoder.com/questionTerminal/a9d0ecbacef9410ca97463e4a5c83be7
C++递归:
//递归终止条件,左右节点都为空 TreeNode* Mirror(TreeNode* pRoot) { if(pRoot == NULL) return pRoot; if(!pRoot->left && !pRoot->right) //递归终止条件 return pRoot; SwapNode(pRoot); Mirror(pRoot->left); Mirror(pRoot->right); return pRoot; //返回根节点 }
//交换当前节点左右 void SwapNode(TreeNode *pRoot) { TreeNode *pTmp = NULL; pTmp = pRoot->left; pRoot->left = pRoot->right; pRoot->right = pTmp; }