数据结构与算法之二叉树: 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;
}
相关推荐
Neil今天也要学习11 分钟前
永磁同步电机无速度算法--永磁同步电机转子位置精确估计的误差抑制方法
算法
Irene199113 分钟前
JavaScript 常见算法复杂度总结(大O表示法)
javascript·算法
开心比对错重要20 分钟前
进程、线程、虚拟线程详解及线程个数设置
java·jvm·算法·面试
爱学大树锯31 分钟前
【594 · 字符串查找 II】
java·开发语言·算法
m0_6924571043 分钟前
图像噪点消除
人工智能·算法
2401_841495641 小时前
【Python高级编程】图着色动态可视化 APP
python·算法·matplotlib·tkinter·回溯法·图着色算法·动态可视化工具
youngee111 小时前
hot100-53搜索旋转排序数组
数据结构·算法·leetcode
烟雨梵兮1 小时前
-刷题小结19
算法
爱学大树锯1 小时前
1361 · 文字并排
算法
Tisfy2 小时前
LeetCode 2483.商店的最少代价:两次遍历 -> 一次遍历
算法·leetcode·题解·遍历