
解法:递归
我的解法是基于先序遍历方法。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* mirrorTree(struct TreeNode* root){
if(root)
{
struct TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
mirrorTree(root->left);
mirrorTree(root->right);
}
return root;
}

这篇博客介绍了如何使用递归方法进行二叉树的镜像翻转,即交换二叉树节点的左右子树。提供的C语言代码展示了先序遍历实现该操作的过程,通过迭代函数`mirrorTree`实现树的翻转。
330

被折叠的 条评论
为什么被折叠?



