码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • [JavaScript 刷题] 树 - 二叉搜索树中的插入操作, leetcode 701


    [JavaScript 刷题] 树 - 二叉搜索树中的插入操作, leetcode 701

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

    题目地址:701. Insert into a Binary Search Tree

    题目

    如下:

    You are given the root node of a binary search tree (BST) and a value to insert into the tree. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.

    Notice that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.

    解题思路

    这一题题目中很明显的就提示说会接受所有的 BST 的构建方法,并且题目的规定也说题目并不会存在重复的数字,这里就挑最简单的方法区解题了。

    二叉搜索树的做法就是比当所有的左子树的值会比当前结点的值小,所有的右子树的值都会比当前结点的值大,因此只要按照这个规律遍历到某个节点为空即可。

    这种解法的平均时间复杂度为 O ( l o g ( n ) ) O(log(n)) O(log(n)),最差情况下,都是左子树/右子树的情况下会成为 O ( n ) O(n) O(n),因此进阶写法可以将其改为平衡二叉搜索树。

    时间复杂度也可以理解成 O ( h ) O(h) O(h),其中 h h h 是树的高度。平衡二叉树规定左右子树的最大高度差不能超过 1,因此可以将时间复杂度限定在 O ( l o g ( n ) ) O(log(n)) O(log(n)) 值中。

    这个实现方法不是最有效的,不过对我来说是最好理解的。

    使用 JavaScript 解题

    /**
     * Definition for a binary tree node.
     * function TreeNode(val, left, right) {
     *     this.val = (val===undefined ? 0 : val)
     *     this.left = (left===undefined ? null : left)
     *     this.right = (right===undefined ? null : right)
     * }
     */
    /**
     * @param {TreeNode} root
     * @param {number} val
     * @return {TreeNode}
     */
    
    //         61
    //        /   \
    //      46      66
    //     /          \
    //   43            88
    //   /
    // 39
    
    var insertIntoBST = function (root, val) {
      const insert = (root) => {
        if (!root) return new TreeNode(val);
    
        if (!root.left && val <= root.val) return (root.left = new TreeNode(val));
    
        if (!root.right && val >= root.val) return (root.right = new TreeNode(val));
    
        if (val < root.val) insert(root.left);
        else insert(root.right);
      };
    
      const res = insert(root);
    
      return root || res;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    Spring中子线程获取RequestAttributes
    〔支付接入〕微信的 h5 支付和 jsapi 支付
    2022年认证杯SPSSPRO杯数学建模D题(第二阶段)食品风味与风味物质求解全过程文档及程序
    关于求直线交点的问题。
    Nginx+Tomcat 搭建负载均衡、动静分离
    Painless脚本在Elasticsearch的高级应用
    blender FLIP-Fluids 流体插件中文帮助文档系列01
    vue 之 Quill编辑器封装
    C++回顾<二>:类-this指针-构造函数-析构函数-隐式/显式调用explicit-初始化列表 -static静态成员变量/函数-常对象|常函数
    R语言安装及基础知识
  • 原文地址:https://blog.csdn.net/weixin_42938619/article/details/125458856
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号