题目要求:翻转一颗二叉树,使得该二叉树的所有左右子树发生翻转;
例:
解:该题可用递归,将左右子树的位置进行翻转置换
void invertBinaryTree(TreeNode *root) {
// write your code here
//算法设计思路:遍历二叉树的所有节点,然后将它的左右儿子进行位置上的对调
//if the root is empty, return;
if(root == NULL)
{
return;
}
TreeNode* tmp = root->left;
root->left = root->right;
root->right = tmp;
invertBinaryTree(root->left);
invertBinaryTree(root->right);
}