zoukankan      html  css  js  c++  java
  • 二叉树镜像

    package Struct;
    
    public class TreeNode {
        public int val = 0;
        public TreeNode left = null;
        public TreeNode right = null;
    
        public TreeNode(int val) {
            this.val = val;
        }
    
        @Override
        public String toString() {
            return "TreeNode [val=" + val + ", left=" + left + ", right=" + right + "]";
        }
    }
     
    package offer;
    
    import Struct.TreeNode;
    
    /**
     * 
     * 二叉树的镜像定义:源二叉树 
                8
               /  
              6   10
             /   / 
            5  7 9  11
            镜像二叉树
                8
               /  
              10   6
             /   / 
            11 9 7   5
     * @author 爱不会绝迹
     *
     */
    public class Problem08 {
        /**
         * 
         * 镜像二叉树
         * @param root
         */
        public static void Mirror(TreeNode root) {
            if(root!=null){
                TreeNode tmp = root.left;
                root.left = root.right;
                root.right = tmp;
                Mirror(root.left);
                Mirror(root.right);
            }
        }
        
        private static int index = -1;
        public static void main(String[] args) {
            Integer[] array = {8,6,5,null,null,7,null,null,10,9,null,null,11,null,null};
            TreeNode tree = createBinTree(array);
    //        inOrder(tree);
    //        postOrder(tree);
            Mirror(tree);
            preOrder(tree);
        }
        private static TreeNode createBinTree(Integer[] array) {
            TreeNode tree = null;
            if(index < array.length-1 && array[++index]!=null){
                tree = new TreeNode(array[index]);
                tree.left = createBinTree(array);
                tree.right = createBinTree(array);
            }
            return tree;
        }
        private static void preOrder(TreeNode root){
            if(root!=null){
                System.out.print(root.val+" ");
                preOrder(root.left);
                preOrder(root.right);
            }
        }
        
        private static void inOrder(TreeNode root){
            if(root!=null){
                preOrder(root.left);
                System.out.print(root.val+" ");
                preOrder(root.right);
            }
        }
        
        private static void postOrder(TreeNode root){
            if(root!=null){
                preOrder(root.left);
                preOrder(root.right);
                System.out.print(root.val+" ");
            }
        }
    }
  • 相关阅读:
    微信小程序地图组件中的include-points怎样缩放视野并将所有坐标点在规定的视野内展示?
    两种常见的mysql集群架构
    layui+oss阿里云附件上传回调报错问题
    redis hash过期时间
    Static和Extern关键字理解
    代理模式
    中介者模式
    访问者模式
    模板方法模式
    迭代器模式
  • 原文地址:https://www.cnblogs.com/yangenyu/p/11299106.html
Copyright © 2011-2022 走看看