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

题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值