镜像树,如图

镜像树

 

 思路:

利用递归的思想,如果一颗树有左右节点,则进行交换,有一个节点为空则退出递归

struct BinaryTreeNode {
    int val;
    BinaryTreeNode* left;
    BinaryTreeNode* right;
};
void mirrorRecursively(BinaryTreeNode* pNode) {
    if (pNode->left == NULL || pNode->right == NULL) {
        return;
    }
    BinaryTreeNode* temp = pNode->left;
    pNode->left = pNode->right;
    pNode->right = temp;

    mirrorRecursively(pNode->left);
    mirrorRecursively(pNode->right);
}

 

相关文章:

  • 2021-12-08
  • 2021-06-04
  • 2021-04-18
  • 2021-07-19
猜你喜欢
  • 2022-12-23
  • 2021-11-15
  • 2021-11-06
相关资源
相似解决方案