zoukankan      html  css  js  c++  java
  • 一周刷完剑指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

  • 相关阅读:
    基于域名的虚拟主机
    用户认证
    部署lnmp
    django开发流程
    sed 和awk的执行方式
    将文本行倒序排列
    《深入理解JAVA虚拟机》----------第二章 JAVA内存区域与内存溢出异常,笔记(上)
    《深入理解JAVA虚拟机》----------第三章 垃圾收集器与内存分配策略,笔记(下)
    《深入理解JAVA虚拟机》----------第三章 垃圾收集器与内存分配策略,笔记(上)
    洛谷P3783 [SDOI2017]天才黑客(前后缀优化建图+虚树+最短路)
  • 原文地址:https://www.cnblogs.com/junge-mike/p/13687245.html
Copyright © 2011-2022 走看看