• 面试题27:二叉树的镜像(C++)


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

    题目描述

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

    例如输入:

         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]

    解题思路

    DFS:先序遍历实现

    (1)递归方法:只要涉及到二叉搜索树,均可以使用递归解决,分析题目发现,二叉树的镜像是通过交换左右子树的节点转换而成的,我们利用二叉树的先序遍历特点递归实现,主要是交换子树左右节点。

    (2)栈方法:二叉树的先序遍历方法有两种,一种是递归方法,另一种是非递归方法,即栈实现,在这里我们使用栈的方法解决,具体思路是将根节点压入栈,然后交换栈顶元素左右子树,将交换后的左右节点分别压入栈的左节点和右节点,然后当栈不为空时返回即可。

    BFS:层次遍历实现

    队列方法:使用队列的方法,一层一层遍历,然后交换队首元素的左右子树

    程序源码

    递归

    /**
     * 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;
            TreeNode* p = root->left;
            root->left = mirrorTree(root->right);
            root->right = mirrorTree(p);
            /*
            if(root == nullptr) return nullptr;
            swap(root->left, root->right);
            mirrorTree(root->left);
            mirrorTree(root->right);
            return root;
            */
            return root;
    
        }
    };

    栈(非递归)

    /**
     * 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;
            stack<TreeNode* > s;
            s.push(root);
            while(!s.empty())
            {
                TreeNode* p = s.top();
                s.pop();
                if(p == nullptr) continue;
                TreeNode* q = p->left;
                p->left = p->right;
                p->right = q;
                s.push(p->left);
                s.push(p->right);
            }
            return root;
        }
    };

    队列

    /**
     * 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;
            queue<TreeNode* > que;
            que.push(root);
            while(!que.empty())
            {
                TreeNode* p = que.front();
                que.pop();
                if(p == nullptr) continue;
                TreeNode* q = p->left;
                p->left = p->right;
                p->right = q;
                que.push(p->left);
                que.push(p->right);            
            }
            return root;
        }
    };
    ----------------------------------- 心之所向,素履所往;生如逆旅,一苇以航。 ------------------------------------------
  • 相关阅读:
    WEEK
    更新yum源
    Centos6.9安装Mysql5.7.18
    gitlab使用
    gitlab安装
    git客户端
    服务器端配置
    错误问题
    服务器端
    01找出数组中重复的数
  • 原文地址:https://www.cnblogs.com/wzw0625/p/12640690.html
Copyright © 2020-2023  润新知