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+" ");
            }
        }
    }
  • 相关阅读:
    CXX解析CSV文件
    linux通过cifs挂载windows共享目录
    oracle生产环境存储过程调试方案
    imp导入库表空间找不到问题记录
    银行怎么盘头寸
    jQuery插件之【jqGrid】常用语法整理-【更新】
    Jquery一些笔记
    request对象的五个集合
    jQuery插件之【jqGrid】常用语法整理-【更新】
    MVC中几种常用ActionResult
  • 原文地址:https://www.cnblogs.com/yangenyu/p/11299106.html
Copyright © 2011-2022 走看看