剑指 Offer 27 二叉树的镜像

Tag: 剑指-Offer Posted on 2022-02-23 16:41:21 Edited on 2022-02-23 16:41:21 Views: 145

概述

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/