zoukankan      html  css  js  c++  java
  • 《程序员代码面试指南》第三章 二叉树问题 先序、中序和后序数组两两结合重构二叉树

    先序、中序和后序数组两两结合重构二叉树

    先序、中序和后序数组两两结合重构二叉树
    

    java代码

    package com.lizhouwei.chapter3;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * @Description:先序、中序和后序数组两两结合重构二叉树
     * @Author: lizhouwei
     * @CreateDate: 2018/4/16 19:53
     * @Modify by:
     * @ModifyDate:
     */
    public class Chapter3_21 {
    
        //前序和中序数组组成二叉树
        public Node preInToTree(int[] pre, int[] in) {
            if (pre == null || in == null) {
                return null;
            }
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < in.length; i++) {
                map.put(in[i], i);
            }
            return preAndIn(pre, 0, pre.length - 1, in, 0, in.length - 1, map);
        }
    
        public Node preAndIn(int[] pre, int preStart, int preEnd, int[] in, int inStart, int inEnd, Map<Integer, Integer> map) {
            if (preStart > preEnd) {
                return null;
            }
            int value = pre[preStart];
            Node head = new Node(value);
            int index = map.get(value);
    
            head.left = preAndIn(pre, preStart + 1, preStart + index - inStart, in, inStart, index - 1, map);
            head.right = preAndIn(pre, preStart + index - inStart + 1, preEnd, in, index + 1, inEnd, map);
    
            return head;
        }
    
        //中序和后序数组组成二叉树
        public Node inPosToTree(int[] in, int[] pos) {
            if (in == null || pos == null) {
                return null;
            }
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < in.length; i++) {
                map.put(in[i], i);
            }
            return inAndPos(in, 0, in.length - 1, pos, 0, pos.length - 1, map);
        }
    
        public Node inAndPos(int[] in, int inStart, int inEnd, int[] pos, int posStart, int posEnd, Map<Integer, Integer> map) {
            if (posStart > posEnd) {
                return null;
            }
            int value = pos[posEnd--];
            Node head = new Node(value);
            int index = map.get(value);
    
            head.left = inAndPos(in, inStart, index - 1, pos, posStart, posStart + index - inStart - 1, map);
            head.right = inAndPos(in, index + 1, inEnd, pos, posStart + index - inStart, posEnd, map);
    
            return head;
        }
    
        //前序和后序数组组成二叉树
        public Node prePosToTree(int[] pre, int[] pos) {
            if (pre == null || pos == null) {
                return null;
            }
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < pos.length; i++) {
                map.put(pos[i], i);
            }
            return preAndPos(pre, 0, pre.length - 1, pos, 0, pos.length - 1, map);
        }
    
        public Node preAndPos(int[] pre, int preStart, int preEnd, int[] pos, int posStart, int posEnd, Map<Integer, Integer> map) {
            if (preStart > preEnd) {
                return null;
            }
            int value = pos[posEnd--];
            Node head = new Node(value);
            if (preStart == preEnd) {
                return head;
            }
            int index = map.get(pre[++preStart]);
    
            head.left = preAndPos(pre, preStart, preStart + index - posStart, pos, posStart, index, map);
            head.right = preAndPos(pre, preStart + index - posStart + 1, preEnd, pos, index + 1, posEnd, map);
    
            return head;
        }
    
        //测试
        public static void main(String[] args) {
            Chapter3_21 chapter = new Chapter3_21();
            int[] pre = {5, 2, 1, 3, 4, 8, 6, 7, 9, 10};
            int[] in = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
            int[] pos = {1, 4, 3, 2, 7, 6, 10, 9, 8, 5};
    
            Node head = chapter.preInToTree(pre, in);
            System.out.println("前序数组:pre = {5, 2, 1, 3, 4, 8, 6, 7, 9, 10}");
            System.out.println("中序数组:in = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}");
            System.out.println("后序数组:pos = {1, 4, 3, 2, 7, 6, 10, 9, 8, 5}");
            System.out.println();
            System.out.println("前序数组和中序数组组成的二叉树:");
            System.out.print("二叉树前序遍历:");
            NodeUtil.preOrder(head);
            System.out.print("二叉树中序遍历:");
            NodeUtil.inOrder(head);
            System.out.println();
            head = chapter.inPosToTree(in, pos);
            System.out.println("后序数组和中序数组组成的二叉树:");
            System.out.print("二叉树后序遍历:");
            NodeUtil.postOrder(head);
            System.out.print("二叉树中序遍历:");
            NodeUtil.inOrder(head);
            System.out.println();
            head = chapter.prePosToTree(pre, pos);
            System.out.println("前序数组和后序数组组成的二叉树:");
            System.out.print("二叉树前序遍历:");
            NodeUtil.preOrder(head);
            System.out.print("二叉树后序遍历:");
            NodeUtil.postOrder(head);
        }
    }
    
  • 相关阅读:
    JAVA8 之 Stream 流(四)
    关于iphone 6s 页面功能不能正常使用问题
    关于ES6语法的 一些新的特性
    微信授权一直跳转
    js 一道题目引发的正则的学习
    关于this在不同使用情况表示的含义
    详细解析arry.map() ,function.apply() 方法
    关于服务器无法在已发送http表头之后设置状态问题
    七牛上传视频并转码
    使用 v-cloak 防止页面加载时出现 vuejs 的变量名
  • 原文地址:https://www.cnblogs.com/lizhouwei/p/8858578.html
Copyright © 2011-2022 走看看