剑指 Offer 27 二叉树的镜像

标签: 剑指 Offer 发布于:2022-02-23 16:41:21 编辑于:2022-02-23 16:41:21 浏览量:775

概述

https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/

https://leetcode.com/problems/invert-binary-tree/

递归法

class Solution {
public:
    TreeNode* mirrorTree(TreeNode* root) {
        if (!root) return nullptr;
        auto temp = root->left;
        root->left = mirrorTree(root->right);
        root->right = mirrorTree(temp);
        return root;
    }
};

未经允许,禁止转载,本文源站链接:https://iamazing.cn/