码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • [JavaScript 刷题] 树 - 从前序与中序遍历序列构造二叉树, leetcode 105


    [JavaScript 刷题] 树 - 从前序与中序遍历序列构造二叉树, leetcode 105

    github repo 地址: https://github.com/GoldenaArcher/js_leetcode,Github 的目录 大概 会更新的更勤快一些。

    题目地址:

    题目

    如下:

    Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.

    解题思路

    想要解这道题,首先得了解一下二叉树的几种遍历方式:二叉树先序、中序、后序及层次四种遍历。

    知道了前序遍历和中序遍历之后,这道题目就比较好拆解了,以题目中的案例来说:

    preorder:

    [3,9,20,15,7]

    inorder:

    [9,3,15,20,7]

    已知 preorder 数组中的第一个值为当前结点(root),inorder 使用 inorder[indexOf(preorder[0])] 可以找到当前结点(root),该值左侧为 root 的左子树,右侧为 root 的右子树,根据这个方法就可以拆分出当前二叉树。

    依旧以提供的案例来说:

    1. root 为 3

      左子树调用 buildTree([9], [9,3])

      这里 preorder 的值剔除了当前的 root,所以取值范围为 (0, min]

      右子树调用 buildTree([20,15,7], [15,20,7])

      取值范围为 (mid, ..., arr.length)

    2. buildTree([9], [9,3]) 的结果返回 TreeNode(3)

    3. buildTree([20,15,7], [15,20,7]) 返回 TreeNode(20)

      其左右子树依旧会继续递归调用 buildTree() 去创建新的子节点

    使用 JavaScript 解题

    // [3,      9,      20,15,7]
    // root    left
    
    // [9,      3,      15,20,7]
    // left     root
    
    var buildTree = function (preorder, inorder) {
      if (!preorder.length || !inorder.length) return null;
    
      const root = new TreeNode(preorder[0]);
      const mid = inorder.indexOf(preorder[0]);
    
      root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid + 1));
      root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));
    
      return root;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
  • 相关阅读:
    解释 Git 的基本概念和使用方式
    Springboot jar运行时,将jar内的文件拷贝到文件系统中
    java街边熟食店卤菜网上商城系统springboot+vue
    作用域及作用域链
    云贝教育 |【DSI】Oracle数据库系列课-葵花宝典技术内幕实战课程
    记录使用Docker Compose 部署《XAPI项目》遇道的问题及解决方案
    七夕小案例:用代码给心爱的她画一个爱心
    USACO Training 1.3 Palindromic Squares
    GBase8s数据库对 STANDARD 或 RAW 结果表排序
    Pick-a-Pic:An open dataset of user preferences for text-to-image generation
  • 原文地址:https://blog.csdn.net/weixin_42938619/article/details/125598353
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号