一周刷完剑指offer-18-二叉树的镜像

二叉树的镜像

1. 题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。

2. 示例

image.png

3. 解题思路

递归的方法:一开始遍历节点,直接交换它的两个子节点,

当交换完所有的非叶子结点的左右子结点之后,就得到了树的镜像

非递归方法: 使用队列,类似于层次遍历

4. Java实现

递归实现

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public void Mirror(TreeNode root) {
        if (root == null){
            return ;
        }
        if (root.left == null && root.right == null){
            return;
        }
        
        TreeNode temp; // 交换左右子树
        temp = root.left;
        root.left = root.right;
        root.right = temp;
        
        Mirror(root.left); // 递归对左子树交换
        Mirror(root.right);
    }
}

非递归实现,使用队列实现,类似于树的层次遍历

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
import java.util.Queue;
import java.util.LinkedList;
 
public class Solution {
    public void Mirror(TreeNode root) {
        if (root == null)
        {
            return;
        }        // 使用队列
        Queue<TreeNode> q = new LinkedList();
        q.offer(root);
        
        while (!q.isEmpty()){
            TreeNode node = q.poll();
            // 交换元素
            TreeNode temp;
            if (node.left != null || node.right != null){
                temp = node.left;
                node.left = node.right;
                node.right = temp;
            }
            
            if (node.left != null){
                q.offer(node.left);
            }
            if (node.right != null){
                q.offer(node.right);
            }
        }
           
    }
    
}

5. Python实现

递归实现

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回镜像树的根节点
    def Mirror(self, root):
        # write code here
        if not root:
            return None 
        if not root.left and not root.right:
            return root 
        root.left, root.right = root.right, root.left 
        self.Mirror(root.left)
        self.Mirror(root.right)

非递归实现,队列

# -*- coding:utf-8 -*-
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回镜像树的根节点
    def Mirror(self, root):
        # write code here
        if not root:
            return None 
        nodeQue = [root]
        while len(nodeQue) > 0:
            nodeNum, count = len(nodeQue), 0
            while nodeNum > count:
                count += 1
                node = nodeQue.pop(0)
                node.left, node.right = node.right, node.left 
                if node.left:
                    nodeQue.append(node.left)
                if node.right:
                    nodeQue.append(node.right)

如果您觉得本文有用,请点个“在看”

image.png

posted @ 2020-09-17 19:56  蜘蛛侠不会飞  阅读(127)  评论(0编辑  收藏  举报

俺的博客

https://blog.csdn.net/qq_40587575

俺的公众号