Leetcode刷题笔记

Leetcode刷题笔记,第1张

深度搜索(递归也一样)
class Solution {
public:
    void towmirrortree(TreeNode* root){
        if(root==NULL)return;
        if(root->left==NULL&&root->right==NULL)return;
        auto temp=root->left;
        root->left=root->right;
        root->right=temp;
        towmirrortree(root->left);
        towmirrortree(root->right);
    }
    TreeNode* mirrorTree(TreeNode* root) {
        towmirrortree(root);
        return root;
    }
};

递归
class Solution {
public:
    TreeNode* mirrorTree(TreeNode* root) {
        if(root==NULL)return NULL;
        if(root->left==NULL&&root->right==NULL)return root;
        auto temp=root->left;
        root->left=mirrorTree(root->right);
        root->right=mirrorTree(temp);
        return root;
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/734523.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存