博客
关于我
剑指offer笔记 07.给出二叉树的前序遍历和中序遍历,重建该二叉树
阅读量:585 次
发布时间:2019-03-11

本文共 1548 字,大约阅读时间需要 5 分钟。

为了解决这个问题,我们需要根据给定的前序遍历和中序遍历结果,重建一个二叉树。前序遍历和中序遍历都是递归的结果,我们可以利用这些结果来确定二叉树的结构。

方法思路

  • 问题分析

    • 前序遍历结果是 [root, left, right],表示根节点访问后,先访问左子树,最后访问右子树。
    • 中序遍历结果是 [left, root, right],表示先访问左子树,接着访问根节点,最后访问右子树。
    • 给定这些结果,我们可以确定每个节点的位置。
  • 关键思路

    • 使用递归来构建树。
    • 每次递归调用时,确定当前根节点的位置,然后分割左右子树。
    • 使用哈希表记录中序遍历中每个节点的位置,以便快速查找。
  • 算法步骤

    • 创建一个递归函数,参数包括当前前序遍历的索引 i,中序遍历的左边界 left 和右边界 right
    • 在函数内部,找到当前根节点在中序遍历中的位置 j
    • 分别构建左子树和右子树,递归调用左子树和右子树的构建函数。
    • 返回构建好的根节点。
  • 解决代码

    import java.util.HashMap;
    import java.util.Map;
    class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) {
    val = x;
    }
    }
    public class Solution {
    private Map
    hashmap = new HashMap<>();
    private int[] preorder;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
    this.preorder = preorder;
    for (int i = 0; i < preorder.length; i++) {
    hashmap.put(inorder[i], i);
    }
    return recur(0, 0, inorder.length - 1);
    }
    private TreeNode recur(int i, int left, int right) {
    if (left > right) {
    return null;
    }
    int j = hashmap.get(preorder[i]);
    TreeNode root = new TreeNode(preorder[i]);
    root.left = recur(i + 1, left, j - 1);
    root.right = recur(i + (j - left) + 1, j + 1, right);
    return root;
    }
    }

    代码解释

    • TreeNode 类:定义了二叉树的节点,包含值、左子树和右子树。
    • buildTree 方法:初始化时,建立哈希表记录中序遍历中每个节点的位置,并调用递归函数构建树。
    • recur 方法:递归函数,根据当前前序索引 i 和中序左右边界 leftright 构建树。
      • 检查边界条件,返回 null 若无子树。
      • 找到当前根节点在中序中的位置 j
      • 分别构建左子树和右子树,递归调用函数。
      • 返回当前根节点。

    这种方法利用了前序和中序遍历的特点,通过递归和哈希表高效地重建二叉树,确保了算法的正确性和效率。

    转载地址:http://vbftz.baihongyu.com/

    你可能感兴趣的文章
    NMF(非负矩阵分解)
    查看>>
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>