剑指offer-重建二叉树(java版)

来源:互联网 发布:java 集合不加泛型 编辑:程序博客网 时间:2024/06/06 12:56

题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并输出它的后序遍历序列。
这里写图片描述

分析:

  • 通过前序序列得到根节点的值
  • 通过中序序列得到左右子树的长度
  • 通过根节点位置和左右子树的长度,能够得到前序序列左右左子树起始位置与结束位置和中序序列左右子树起始位置和结束位置。
  • 不断递归,得到二叉树
    前序

中序

import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;/** * 重建二叉树: 题目:输入某二叉树的前前序遍历和中序遍历的结果,请重建该二叉树。 * */public class 面试题7 {    public static int[] pre;    public static int[] in;    public static boolean canBuild = true;    public static BinaryTreeNode ConstructCore(int startPreorder, int endPreorder, int startInorder, int endInorder) {        int root_value = pre[startPreorder];        BinaryTreeNode root = new BinaryTreeNode();        root.value = root_value;        if (startPreorder == endPreorder) {            if (startInorder == endInorder && pre[startPreorder] == in[startInorder])                return root;            else {                canBuild = false;                return null;            }        }        int rootInorder = startInorder;        while (rootInorder <= endInorder && in[rootInorder] != root_value)            rootInorder++;        if (rootInorder > endInorder) {            canBuild = false;            return null;        }        int leftLength = rootInorder - startInorder; // 左子树长度        int rightLength = endInorder - rootInorder; // 右子树长度        if (leftLength > 0)            root.left = ConstructCore(startPreorder + 1, startPreorder + leftLength, startInorder, rootInorder - 1);        if (rightLength > 0)            root.right = ConstructCore(endPreorder - rightLength + 1, endPreorder, rootInorder + 1, endInorder);        return root;    }    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        while (scanner.hasNext()) {            canBuild = true;            int length = scanner.nextInt();            pre = new int[length];            in = new int[length];            for (int i = 0; i < length; i++)                pre[i] = scanner.nextInt();            for (int i = 0; i < length; i++)                in[i] = scanner.nextInt();            BinaryTreeNode root = ConstructCore(0, length - 1, 0, length - 1);            // show(root);            show_after(root);            System.out.println();        }        scanner.close();    }    public static void show(BinaryTreeNode root) {        Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>();        queue.add(root);        while (!queue.isEmpty()) {            BinaryTreeNode node = queue.poll();            if (node.left != null)                queue.add(node.left);            if (node.right != null)                queue.add(node.right);            System.out.print(node.value + " ");        }    }    public static void show_after(BinaryTreeNode root) {        if (canBuild == false) {            System.out.print("No");            return;        }        if (root.left != null)            show_after(root.left);        if (root.right != null)            show_after(root.right);        System.out.print(root.value + " ");    }}class BinaryTreeNode {    int value;    BinaryTreeNode left;    BinaryTreeNode right;}
原创粉丝点击