zoukankan      html  css  js  c++  java
  • 剑指 Offer 27. 二叉树的镜像

    请完成一个函数,输入一个二叉树,该函数输出它的镜像。

    例如输入:

         4
       /  
      2     7
     /   /
    1   3 6   9

    镜像输出:

         4
       /  
      7     2
     /   /
    9   6 3   1

    示例 1:

    输入:root = [4,2,7,1,3,6,9]
    输出:[4,7,2,9,6,3,1]
    

    限制:

    0 <= 节点个数 <= 1000

    错解:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode* mirrorTree(TreeNode* root) {
            if(root){
                mirrorTree(root->left);
                mirrorTree(root->right);
                if(root->left && root->right)
                    swap(root->left->val, root->right->val);
            }
            return root;
        }
    };
    输入
    [4,2,7,1,3,6,9]
    输出
    [4,7,2,3,1,9,6]
    预期结果
    [4,7,2,9,6,3,1]
     
    法一:
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        TreeNode* mirrorTree(TreeNode* root) {
            if(root == nullptr) return nullptr;
            if(root->left == nullptr && root->right == nullptr) return root;
    
            TreeNode* temp = root->left;
            root->left = root->right;
            root->right = temp;
    
            if(root->left) mirrorTree(root->left);
            if(root->right) mirrorTree(root->right);
    
            return root;
        }
    };

    法二:

    class Solution {
    public:
        TreeNode* mirrorTree(TreeNode* root) {
            if (root == nullptr) {
                return nullptr;
            }
            TreeNode* left = mirrorTree(root->left);
            TreeNode* right = mirrorTree(root->right);
            root->left = right;
            root->right = left;
            return root;
        }
    };
    
    作者:LeetCode-Solution
    链接:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/solution/er-cha-shu-de-jing-xiang-by-leetcode-sol-z44i/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    时间复杂度:O(N),其中 N 为二叉树节点的数目。我们会遍历二叉树中的每一个节点,对每个节点而言,我们在常数时间内交换其两棵子树。

    空间复杂度:O(N)。使用的空间由递归栈的深度决定,它等于当前节点在二叉树中的高度。在平均情况下,二叉树的高度与节点个数为对数关系,即 O(logN)。而在最坏情况下,树形成链状,空间复杂度为 O(N)。

    作者:LeetCode-Solution
    链接:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/solution/er-cha-shu-de-jing-xiang-by-leetcode-sol-z44i/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 相关阅读:
    redis缓存数据
    VMware克隆服务器
    Java去掉Html标签的方法
    设计模式学习笔记:一、代理模式(动态代理实现)
    设计模式学习笔记:一、代理模式(动态代理步骤+举例)
    Python教程资源
    Lucene入门
    Oracle入门
    Sql入门
    将web工程署到Linux简单实现
  • 原文地址:https://www.cnblogs.com/AbsolutelyPerfect/p/14940138.html
Copyright © 2011-2022 走看看