数据结构与算法之二叉树: LeetCode 701. 二叉搜索树中的插入操作 (Ts版)

二叉搜索树中的插入操作

描述

  • 给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树
  • 返回插入后二叉搜索树的根节点。 输入数据 保证 ,新值和原始二叉搜索树中的任意节点值都不同
  • 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可
  • 你可以返回 任意有效的结果

示例 1

复制代码
输入:root = [4,2,7,1,3], val = 5
输出:[4,2,7,1,3,5]

解释:另一个满足题目要求可以通过的树是:

示例 2

复制代码
输入:root = [40,20,60,10,30,50,70], val = 25
输出:[40,20,60,10,30,50,70,null,null,25]

示例 3

复制代码
输入:root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
输出:[4,2,7,1,3,5]

提示

  • 树中的节点数将在 0, 1 0 4 10\^4 104的范围内
    • 1 0 8 10^8 108 <= Node.val <= 1 0 8 10^8 108
  • 所有值 Node.val 是 独一无二 的
    • 1 0 8 10^8 108 <= val <= 1 0 8 10^8 108
  • 保证 val 在原始BST中不存在

Typescript 版算法实现

1 ) 方案1:模拟

ts 复制代码
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
    if (root === null) return new TreeNode(val);
    let pos = root;
    while (pos !== null) {
        if (val < pos.val) {
            if (pos.left === null) {
                pos.left = new TreeNode(val);
                break;
            } else {
                pos = pos.left;
            }
        } else {
            if (pos.right === null) {
                pos.right = new TreeNode(val);
                break;
            } else {
                pos = pos.right;
            }
        }
    }
    return root;
};

2 ) 方案2:递归

ts 复制代码
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
    // 如果当前树为空,说明找到了插入位置,创建新节点并返回
    if (!root) return new TreeNode(val);

    // 如果要插入的值小于当前节点的值,在左子树中进行插入操作
    if (val < root.val) {
        root.left = insertIntoBST(root.left, val);
    } else {
        // 如果要插入的值大于等于当前节点的值,在右子树中进行插入操作
        root.right = insertIntoBST(root.right, val);
    }

    // 返回当前节点,保持树的结构
    return root;
}
相关推荐
卷福同学2 小时前
不用服务器,不用配环境,我10分钟上线了一个AI Agent
人工智能·后端·算法
至乐活着4 小时前
深入解析跳表SkipList:原理、实现与性能优化实战
数据结构·算法·跳表·skiplist·java实现
Jerry5 小时前
LeetCode 383. 赎金信
算法
ai产品老杨5 小时前
H264 H265视频分析常见问题和排查清单
人工智能·算法·音视频
Jerry5 小时前
LeetCode 454. 四数相加 II
算法
可编程芯片开发6 小时前
基于CPS-SPWM链式STATCOM系统在电压不平衡环境下控制策略的simulink建模与仿真
算法
Jerry6 小时前
LeetCode 202. 快乐数
算法
hans汉斯7 小时前
基于改进交叉熵损失函数与Transformer的心电信号高风险分类研究
功能测试·深度学习·算法·yolo·目标检测·分类·transformer
Jerry7 小时前
LeetCode 349. 两个数组的交集
算法
YuK.W8 小时前
Leetcode100: 70.爬楼梯、118.杨辉三角、198.打家劫舍
java·算法·leetcode