• 剑指offer(Java版)第五题:输入某二叉树的前序遍历和中序遍历的结果, 请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 例如输入前序遍历序列{1, 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1, 5, 3, 8, 6}, 则重建出其二叉树并输出它的头结点。


    /*
    输入某二叉树的前序遍历和中序遍历的结果,
    请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
    例如输入前序遍历序列{1, 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1, 5, 3, 8, 6},
    则重建出其二叉树并输出它的头结点。
    */

    import java.util.*;

    public class Class6 {

    class TreeNode{
    int val;
    public TreeNode(int val){
    this.val = val;
    }
    TreeNode left;
    TreeNode right;
    }

    public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
    if (pre == null || in == null || pre.length <= 0 || in.length <= 0 || pre.length != in.length) {
    throw new RuntimeException("数据有误!");
    }
    return construct(pre, in, 0, pre.length - 1, 0, in.length - 1);
    }

    public TreeNode construct(int[] pre, int[] in, int pStart, int pEnd, int iStart, int iEnd) {
    TreeNode root = new TreeNode(pre[pStart]);
    if (pStart == pEnd && iStart == iEnd) {
    if (pre[pStart] != in[iStart])
    throw new RuntimeException("数据有误!");
    return root;
    }
    int index = iStart; // 用于记录中序遍历序列中根结点的位置
    while (root.val != in[index] && index <= iEnd) {
    index++;
    }
    if (index > iEnd)
    throw new RuntimeException("数据有误!");
    int leftLength = index - iStart;
    if (leftLength > 0) {
    root.left = construct(pre, in, pStart + 1, pStart + leftLength, iStart, index - 1);
    }
    if (leftLength < iEnd - iStart) {
    root.right = construct(pre, in, pStart + leftLength + 1, pEnd, index + 1, iEnd);
    }
    return root;
    }
    private void pre1(TreeNode node){
    if (node == null)
    return;
    System.out.print(node.val);
    pre1(node.left);
    pre1(node.right);
    }
    private void in1(TreeNode node){
    if (node == null)
    return;
    in1(node.left);
    System.out.print(node.val);
    in1(node.right);
    }
    private void test(){
    int[] pre = {1, 2, 4, 7, 3, 5, 6, 8};
    int[] in = {4, 7, 2, 1, 5, 3, 8, 6};
    TreeNode root = reConstructBinaryTree(pre, in);
    System.out.println("前序遍历:");
    pre1(root);
    System.out.println();
    System.out.println("中序遍历:");
    in1(root);
    }

    public static void main(String[] args) {
    Class6 c = new Class6();
    c.test();
    }
    }

  • 相关阅读:
    c语言中输出char类型所占据的位数(因编译器而异)
    C语言中输出不同变量类型的长度(单位为字节,1字节等于8位)
    C语言中作用域
    python 中字符串 内置函数 find
    python 中字符串拆分可直接赋值给变量名(列表中的元素可以直接赋值给变量)
    telnet协议
    LeetCode 图解 | 37.解数独
    新年锦鲤!自费送128G的 iPhone 11 !
    计算机领域有哪些常见的比赛
    Git 使用教程:最详细、最傻瓜、最浅显、真正手把手教!(万字长文)
  • 原文地址:https://www.cnblogs.com/zhuozige/p/12419111.html
Copyright © 2020-2023  润新知