数据结构与算法之二叉树: 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;
}
相关推荐
吴可可1231 分钟前
Teigha中OdGe几何库详解及C#使用
算法
爱喝水的鱼丶12 分钟前
SAP-ABAP:变量、常量、结构与内表声明(10篇博客合集) 第六篇:ABAP 7.40+新特性:声明语法的简化写法与兼容注意事项
运维·服务器·开发语言·学习·算法·sap·abap
国科安芯37 分钟前
AS32S601商业航天级抗辐照MCU芯片:架构设计与技术特性研究
单片机·嵌入式硬件·算法·安全·架构·risc-v
菜菜的顾清寒1 小时前
力扣HOT100(34)图论-岛屿数量
算法·leetcode·图论
圣保罗的大教堂1 小时前
leetcode 2657. 找到两个数组的前缀公共数组 中等
leetcode
名字不好奇1 小时前
大模型的思考模式:它真的在“想“吗?
人工智能·算法
Run_Teenage1 小时前
算法模板:输入输出,并查集
java·开发语言·算法
高一学习c++会秃头吗1 小时前
操作系统内存块分配算法
算法
洛水水1 小时前
【力扣100题】57.合并区间
算法·leetcode
圣保罗的大教堂1 小时前
leetcode 33. 搜索旋转排序数组 中等
leetcode