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

    题目描述

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

    输入描述:

    二叉树的镜像定义:源二叉树
        	    8     
        	   /  
        	  6   10
        	 /   / 
        	5  7 9 11
        	镜像二叉树
        	    8
        	   /  
        	  10   6
        	 /   / 
        	11 9 7  5
    

    思路

    先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子节点,当交换完所有的非叶子结点的左右子结点之后,就得到了树的镜像。

    思路一:

    递归

    思路二:

    借助栈遍历二叉树

    代码实现

    package Tree;
    
    import java.util.ArrayDeque;
    import java.util.ArrayList;
    import java.util.Deque;
    
    /**
     * 二叉树的镜像
     * 操作给定的二叉树,将其变换为源二叉树的镜像。
     * 二叉树的镜像定义:源二叉树
     * 8
     * /  
     * 6   10
     * /   / 
     * 5  7 9 11
     * 镜像二叉树
     * 8
     * /  
     * 10   6
     * /   / 
     * 11 9 7  5
     * 思路:
     * 先前序遍历这棵树的每个结点,如果遍历到的结点有子结点,就交换它的两个子节点,
     * 当交换完所有的非叶子结点的左右子结点之后,就得到了树的镜像
     */
    public class Solution15 {
        public static void main(String[] args) {
            Solution15 solution15 = new Solution15();
            int[] array = {8, 6, 10, 5, 7, 9, 11};
            TreeNode treeNode = solution15.createBinaryTreeByArray(array, 0);
            System.out.println(treeNode.left.right.val);
            solution15.Mirror_2(treeNode);
            System.out.println(treeNode.left.right.val);
    
        }
    
        /**
         * 用栈遍历二叉树
         *
         * @param root
         */
        public void Mirror_2(TreeNode root) {
            if (root == null) {
                return;
            }
            Deque<TreeNode> deque = new ArrayDeque<>();
            deque.push(root);
            while (!deque.isEmpty()) {
                TreeNode node = deque.pop();
                if (node.left != null || node.right != null) {
                    TreeNode temp = node.left;
                    node.left = node.right;
                    node.right = temp;
                }
                if (node.left != null) {
                    deque.push(node.left);
                }
                if (node.right != null) {
                    deque.push(node.right);
                }
            }
    
    
        }
    
        /**
         * 递归
         *
         * @param root
         */
        public void Mirror(TreeNode root) {
            if (root == null) {
                return;
            }
            if (root.left == null && root.right == null) {
                return;
            }
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
            if (root.left != null) {
                Mirror(root.left);
            }
            if (root.right != null) {
                Mirror(root.right);
            }
        }
    
        public class TreeNode {
            int val = 0;
            TreeNode left = null;
            TreeNode right = null;
    
            public TreeNode(int val) {
                this.val = val;
    
            }
    
        }
    
        public TreeNode createBinaryTreeByArray(int[] array, int index) {
            TreeNode tn = null;
            if (index < array.length) {
                int value = array[index];
                tn = new TreeNode(value);
                tn.left = createBinaryTreeByArray(array, 2 * index + 1);
                tn.right = createBinaryTreeByArray(array, 2 * index + 2);
                return tn;
            }
            return tn;
        }
    }
    
    
  • 相关阅读:
    用 ArcMap 发布 ArcGIS Server FeatureServer Feature Access 服务 PostgreSQL 版本
    ArcMap 发布 ArcGIS Server OGC(WMSServer,MapServer)服务
    ArcScene 创建三维模型数据
    ArcMap 导入自定义样式Symbols
    ArcMap 导入 CGCS2000 线段数据
    ArcMap 导入 CGCS2000 点坐标数据
    ArcGis Server manager 忘记用户名和密码
    The view or its master was not found or no view engine supports the searched locations
    python小记(3)操作文件
    pytest(2) pytest与unittest的区别
  • 原文地址:https://www.cnblogs.com/wupeixuan/p/8623179.html
Copyright © 2011-2022 走看看